java计算器程序代码
2017-04-20 12:15
330 查看
/*
* 描述:计算器
*/
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);
}
}
* 描述:计算器
*/
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);
}
}
相关文章推荐
- 把散乱的SQL赶出程序代码中
- 关于Basic程序解释器及编译原理的简单化(1)---Basic器的语法分析及主要代码
- asp程序权限控制的代码
- 二级域名原理以及程序代码
- 到现在为止,成功调试完成我的第一个Java程序,意味着我也要进行Java这一行了,请看我的代码!
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 通用 文件保存至数据库,从数据库写入磁盘 程序代码
- 一段分页程序实例代码!
- 分页程序代码(mysql数据库的简单分页)
- 在VB程序中,通过代码改变Combo控件只读属性Style值
- 方便 Weblog 显示加亮语法 PHP 代码的小程序
- 为汉字注音的C程序代码
- SAP ABAP/4学习---如何给写好的一个程序分配事务代码,用户可以直接通过事务码来访问程序.或者加入收藏夹(9)
- 热情推荐:开源程序/代码挖掘之codeproject (2)
- 关于文件复制的程序代码(C++Builder)
- C Java PHP Perl Python 的程序代码美化工具
- 【原创醒目】为汉字注音的C程序代码,支持win32下C编译的都能用!
- 如何建立程序代码包的联接?
- 如何用 C 注册 windows 服务程序 (分析 Java Service Wrapper 代码)
- Delphi 程序经常用到的公共代码 Tools.pas