import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Calculators extends JFrame implements ActionListener{
int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;
int frameWidth=310;
int frameHeight=240;
Point p=new Point((screenWidth-frameHeight)/2,(screenHeight-frameHeight)/2);
private double num=0.0;
private String operator="=";
private boolean firstDigit=true;
private boolean operatorValidFlag=true;
private final String[] btnTexts={
"7","8","9","/","sqrt","4","5","6","*","1/x","1","2",
"3","-","%","0",".","+/-","+","="};
private static JTextField enterNum=new JTextField("0");
private JButton backSButton=new JButton("backsplace");
private JButton clearButton=new JButton("clear");
private JButton[] keys=new JButton[btnTexts.length];
public Calculators(){
super();
enterNum.setHorizontalAlignment(JTextField.RIGHT);
enterNum.setEditable(false);
enterNum.setBackground(Color.white);
Container cp=this.getContentPane();
JPanel p1=new JPanel(new GridLayout(1,2));
p1.add(backSButton);
p1.add(clearButton);
JPanel p2=new JPanel(new GridLayout(2,1));
p2.add(enterNum);
p2.add(p1);
JPanel p3=new JPanel(new GridLayout(4,5,3,3));
for(int i=0;i<btnTexts.length;i++){
keys[i]=new JButton(btnTexts[i]);
keys[i].addActionListener(this);
p3.add(keys[i]);
keys[i].setForeground(Color.blue);
}
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
cp.add(p2,BorderLayout.NORTH);
cp.add(p3,BorderLayout.CENTER);
this.pack();
backSButton.addActionListener(this);
clearButton.addActionListener(this);
this.setSize(frameWidth,frameHeight);
this.setLocation(p);
this.setTitle("Calculator");
this.setResizable(false);
this.setVisible(true);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
dispose();
}
});
}
public void actionPerformed(ActionEvent ae){
String label=ae.getActionCommand();
if(label.equals(backSButton)){
handleBackSplace();
}
else if(label.equals(clearButton)){
enterNum.setText("0");
}
else if("0123456789.".indexOf(label)>=0){
handleNumber(label);
}
else {
handleOperator(label);
}
}
private void handleBackSplace(){
String str=enterNum.getText();
int i=str.length();
if(i>0){
str=str.substring(0,i-1);
if(str.length()==0){
enterNum.setText("0");
firstDigit=true;
operator="=";
}
else{
enterNum.setText(str) ;
}
}
}
private void handleNumber(String key){
if(firstDigit){
enterNum.setText(key);
}
else if(key.equals(".")&&(enterNum.getText().indexOf(".")<0)){
enterNum.setText(enterNum.getText()+key);
}
else if(!key.equals(".")){
enterNum.setText(enterNum.getText()+key);
}
firstDigit=false;
}
private void handleOperator(String key){
if(key.equals("/")){
if(getNumberFromText()==0.0){
operatorValidFlag=false;
enterNum.setText("dividend nunber can not be zero");
}
else{
num/=getNumberFromText();
}
}
else if(key.equals("1/x")){
if(num==0.0){
operatorValidFlag=false;
enterNum.setText("zero has no reciprocal");
}
else{
num=1/num;
}
}
else if(key.equals("+/-")){
num=(-1)*num;
}
else if(key.equals("+")){
num+=getNumberFromText();
}
else if(key.equals("-")){
num-=getNumberFromText();
}
else if(key.equals("*")){
num*=getNumberFromText();
}
else if(key.equals("=")){
num=getNumberFromText();
}
else if(key.equals("sqrt")){
num=Math.sqrt(num);
}
if(operatorValidFlag){
long t1;
double t2;
t1 = (long) num;
t2 = num - t1;
if (t2 == 0) {
enterNum.setText(String.valueOf(t1));
} else {
enterNum.setText(String.valueOf(num));
}
}
operator=key;
firstDigit=true;
operatorValidFlag=true;
}
private double getNumberFromText(){
double number=0.0;
try{
number=Double.valueOf(enterNum.getText()).doubleValue();
}
catch(NumberFormatException e){
e.printStackTrace();
}
return number;
}
public static void main(String[] args) {
Calculators cal=new Calculators();
}
}