package com.project.clock;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calc implementsActionListener{
JTextFieldobjText = new JTextField();
intintOperator = 0;
StringstrBefore = "";
StringstrAfter = "";
StringstrResult = "";
booleanblnFirst = true;
booleanblnResult = false;
booleanblnZero = false;
privatevoid init(){
JFrameobjFrame = new JFrame();
objFrame.setSize(200,280);
objFrame.setResizable(false);
objFrame.setDefaultCloseOperation(3);
objFrame.setLocationRelativeTo(null);
//GridLayoutFlowLayout BorderLayout
objFrame.setLayout(newGridLayout(5,1));
String[]strBtnName ={"1","2","3","+","4","5","6","-","7","8","9","*","0","C","=","/"};
JPanelobjP1 = new JPanel();
JPanelobjP2 = new JPanel();
JPanelobjP3 = new JPanel();
JPanelobjP4 = new JPanel();
JButtonobjBtn;
for(inti=0;i<strBtnName.length;i++){
objBtn= new JButton(strBtnName[i]);
objBtn.addActionListener(this);
if(i<=3){
objP1.add(objBtn);
}elseif(i>=4&&i<=7){
objP2.add(objBtn);
}elseif(i>=8&&i<=11){
objP3.add(objBtn);
}elseif(i>=12&&i<=15){
objP4.add(objBtn);
}
}
objFrame.add(objText);
objFrame.add(objP1);
objFrame.add(objP2);
objFrame.add(objP3);
objFrame.add(objP4);
objFrame.setVisible(true);
}
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
newCalc().init();
}
privatevoid continueOperator(){
if(blnFirst){
blnFirst= false;
strBefore= objText.getText();
objText.setText("");
}else{
if(!blnResult){
strAfter= objText.getText();
}
if(!strBefore.equals("")&&!strAfter.equals("")){
switch(intOperator){
case1:
strResult= String.valueOf(Integer.parseInt(strBefore) + Integer.parseInt(strAfter));
break;
case2:
strResult= String.valueOf(Integer.parseInt(strBefore) - Integer.parseInt(strAfter));
break;
case3:
strResult= String.valueOf(Integer.parseInt(strBefore) * Integer.parseInt(strAfter));
break;
case4:
strResult= String.valueOf(Integer.parseInt(strBefore) / Integer.parseInt(strAfter));
break;
}
objText.setText(strResult);
blnResult= true;
strBefore= strResult;
strAfter= "";
strResult= "";
}
}
}
privatevoid equal(){
if(!blnResult){
strAfter= objText.getText();
}
if(!strBefore.equals("")&&!strAfter.equals("")){
switch(intOperator){
case1:
strResult= String.valueOf(Integer.parseInt(strBefore) + Integer.parseInt(strAfter));
objText.setText(strResult);
blnResult= true;
strBefore= strResult;
strAfter= "";
strResult= "";
blnFirst= true;
break;
case2:
strResult= String.valueOf(Integer.parseInt(strBefore) - Integer.parseInt(strAfter));
objText.setText(strResult);
blnResult= true;
strBefore= strResult;
strAfter= "";
strResult= "";
blnFirst= true;
break;
case3:
strResult= String.valueOf(Integer.parseInt(strBefore) * Integer.parseInt(strAfter));
objText.setText(strResult);
blnResult= true;
strBefore= strResult;
strAfter= "";
strResult= "";
blnFirst= true;
break;
case4:
if(Integer.parseInt(strAfter)!=0){
strResult= String.valueOf(Integer.parseInt(strBefore) / Integer.parseInt(strAfter));
objText.setText(strResult);
blnResult= true;
strBefore= strResult;
strAfter= "";
strResult= "";
blnFirst= true;
}else{
blnZero= true;
blnFirst= true;
objText.setText("");
}
break;
}
}
}
@Override
publicvoid actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
if(e.getActionCommand().charAt(0)>='0'&&e.getActionCommand().charAt(0)<='9'){
if(blnZero){
objText.setText("");
blnZero= false;
}
if(blnResult){
objText.setText("");
blnResult= false;
}
objText.setText(objText.getText().concat(e.getActionCommand()));
}else{
switch(e.getActionCommand().charAt(0)){
case'+':
continueOperator();
intOperator= 1;
break;
case'-':
continueOperator();
intOperator= 2;
break;
case'*':
continueOperator();
intOperator= 3;
break;
case'/':
continueOperator();
intOperator= 4;
break;
case'C':
intOperator= 0;
strBefore= "";
strAfter= "";
strResult= "";
blnFirst= true;
blnResult= false;
blnZero= false;
objText.setText("");
break;
case'=':
equal();
break;
}
}
}