java计算器程序代码

/*
 * 描述:计算器
 */
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);
}


}

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值