[ 永远的UNIX::UNIX技术资料的宝库 ]

首页 > 编程技术 > Java > 正文
 

利用java做一个简单的计算器

来源:不详 (2006-06-01 16:51:43)

共两个类。还只是完成+、-、×、÷运算而已。

GUI只是用了AWT,很简单,相信一看就能懂了。
Calculator.java

public class Calculator{
private String result = "0";
private int op = 0,add = 1,sub = 2,mul = 3,div = 4;

private double stringToDouble(String x){
double y = Double.parseDouble(x);
return y;
}
private void operate(String x){
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op){
case 0:
result = x;
break;
case 1:
result = String.valueOf(y+x1);
break;
case 2:
result = String.valueOf(y-x1);
break;
case 3:
result = String.valueOf(y*x1);
break;
case 4:
if(x1!=0){
result = String.valueOf(y/x1);
}else{
result = "The divisor can't be zero!";
}
break;
}
}

public String opAdd(String x){
operate(x);
op = add;
return result;
}
public String opSubtract(String x){
operate(x);
op = sub;
return result;
}
public String opMultiply(String x){
operate(x);
op = mul;
return result;
}
public String opDivide(String x){
operate(x);
op = div;
return result;
}
public String opEquals(String x){
operate(x);
op = 0;
return result;
}
public void opClean(){
op = 0;
result = "0";
}
}

-------------------------------------------------------------------

第二个
CalculatorGUI.java

import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;

public class CalculatorGUI{
private Frame f;
private Panel p1,p2;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;
private TextField tf;
private String s,op;
private Calculator cal = new Calculator();
private boolean ifOp;

public CalculatorGUI(){
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();

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");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");

tf = new TextField(25);
tf.setEditable(false);


}

public void launchFrame(){
f.setSize(220,160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,4));

b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());

p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2,BorderLayout.SOUTH);
f.setVisible(true);
}

public void setTextFieldText_Temp(){
if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){
tf.setText(tf.getText()+s);
}else{
tf.setText((tf.getText()+s).substring(0,15));
}
}
public void setTextFieldText(){
if(ifOp){
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
}else{
setTextFieldText_Temp();
}
}

public static void main(String[] args){
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}

class myWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}

class setOperator_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
}else if(op.equals("-")){
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
}else if(op.equals("*")){
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
}else if(op.equals("/")){
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
}else if(op.equals("=")){
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
(http://www.fanqiang.com)



 
 相关文章
集成JSP、PHP和JAVA于一体的开发环境 2005-01-18 16:50:01
Redhat linux 9下让PHP支持java类 2005-01-21 11:00:53
在C、JAVA、PHP中操作postgreSql 2005-01-24 15:31:35
JavaBean 介绍 2005-01-27 17:49:58
Sun发布 Java Desktop 3系统 2005-02-21 17:09:51
IBM推动PHP发展,是否寻求Java替身? 2005-02-28 11:49:58
Eclipse 照亮Java众生 扩大地盘 2005-03-02 09:35:34
分析: Sun简化Java授权 开源之门只开一道缝 2005-03-17 15:26:00
Open Solaris能否复制Java的成功 2005-03-31 10:02:43
Sun CEO访华宣讲Solaris/Java 与曙光结盟 2005-04-11 10:21:15
Apache计划开发开源版Java Sun表示欢迎 2005-05-11 08:50:50
Firefox又现2极危缺陷 建议关闭JavaScript 2005-05-11 08:51:09
JAX2005-业界和社区的Java盛会 2005-05-12 11:05:54
脚本语言在企业软件领域崛起 会威胁Java吗? 2005-05-17 08:57:01
取悦Java编程人员 Macromedia要走开源之路 2005-06-07 09:14:40
LAMP挑战.Net、Java 商业软件呈三足鼎立 2005-06-15 11:14:31
Sun推Java软件分享项目 仍不是真正开源 2005-06-22 10:05:01
JBoss推Java升级软件 增添对业界标准支持 2005-06-22 22:52:06
BEA推出基于Java的高速芯片 对抗IBM的进攻 2005-06-27 09:14:07
JavaOne 大会今天开展,NoKia抢尽风头 2005-06-27 10:48:55
 

★  感谢所有的作者为我们学习技术知识提供了一条捷径  ★
www.fanqiang.com