/*
* 描述:计算器
*/
package cn.tx;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorFrame extends Frame
implements ActionListener {
TextField text;
Button
b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bAdd,bSub,bMult,bDiv,b
Equ,bC;
Panel p1,p2,p3,p4,p5;
String s,sign;
double a,b,result;
Calculaor cal;
CalculatorFrame(){
s = new String();
sign = new String();
text = new TextField(20);
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bAdd = new Button("+");
bSub = new Button("-");
bMult = new Button("*");
bDiv = new Button("/");
bEqu = new Button("=");
bC = new Button("C");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
this.setLayout(new GridLayout(5,1));
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
p1.add(text);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bAdd);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(bSub);
p4.add(b7);
p4.add(b8);
p4.add(b9);
p4.add(bMult);
p5.add(bC);
p5.add(b0);
p5.add(bEqu);
p5.add(bDiv);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bAdd.addActionListener(this);
bSub.addActionListener(this);
bMult.addActionListener(this);
bDiv.addActionListener(this);
bC.addActionListener(this);
bEqu.addActionListener(this);
//关闭
this.addWindowListener(new WindowAdapter(){
public void
windowClosing(WindowEvent we){
System.exit(0);
}
});
cal = new Calculaor();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
s=s+"1";
text.setText(s);
}
if(e.getSource()==b2){
s=s+"2";
text.setText(s);
}
if(e.getSource()==b3){
s=s+"3";
text.setText(s);
}
if(e.getSource()==b4){
s=s+"4";
text.setText(s);
}
if(e.getSource()==b5){
s=s+"5";
text.setText(s);
}
if(e.getSource()==b6){
s=s+"6";
text.setText(s);
}
if(e.getSource()==b7){
s=s+"7";
text.setText(s);
}
if(e.getSource()==b8){
s=s+"8";
text.setText(s);
}
if(e.getSource()==b9){
s=s+"9";
text.setText(s);
}
if(e.getSource()==b0){
s=s+"0";
text.setText(s);
}
if(e.getSource()==bAdd){
a=Double.parseDouble
(text.getText());
sign="+";
//text.setText("");
s="";
}
if(e.getSource()==bSub){
a=Double.parseDouble
(text.getText());
sign="-";
//text.setText("");
s="";
}
if(e.getSource()==bMult){
a=Double.parseDouble
(text.getText());
sign="*";
//text.setText("");
s="";
}
if(e.getSource()==bDiv){
a=Double.parseDouble
(text.getText());
sign="/";
//text.setText("");
s="";
}
if(e.getSource()==bEqu){
b=Double.parseDouble
(text.getText());
if(sign.equals("+")){
result = cal.add(a,
b);
}else if(sign.equals("-")){
result =
cal.subtraction(a, b);
}else if(sign.equals("*")){
result =
cal.multiplication(a, b);
}else if(sign.equals("/")){
result =
cal.division(a, b);
}
text.setText(result+"");
}
if(e.getSource()==bC){
text.setText("");
sign="";
a=0;
b=0;
result=0;
s="";
}
}
}
/*
* 描述:计算功能实现
*/
package cn.tx;
public class Calculaor {
public double add(double a,double b){
return a+b;
}
public double subtraction(double a,double b){
return a-b;
}
public double multiplication(double a,double b){
return a*b;
}
public double division(double a,double b){
return a/b;
}
}
/*
*描述:实现类
*/
package cn.tx;
public class TestMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
CalculatorFrame f = new CalculatorFrame();
f.setSize(350,500);
f.setVisible(true);
}
}