一个简单的java计算器源码分析

来源:互联网 发布:淘宝直播卖衣服在哪里 编辑:程序博客网 时间:2024/04/30 16:02

读朱仲杰老师的书有一段时间了,个人觉得他的《java2全方位学习(J2SE增修版)》很不错,应该很适合初学者吧。比较惭愧的是我的进展还比较缓慢,学到GUI部分了,前边的习题倒是跟着作了些,都还比较基础,看来是该修炼“内功心法”了。正好AWT元件部分课后习题要求编写一个简单的计算器。其实我觉得GUI界面实现倒不是问题,在实践中不断体会用OOP方法解决问题很关键。

    在网上找了些代码参考,自己在电脑上敲了敲,今天又拿出来作了下修改并把关键部分都加了注释,以便更好的理解。没有涉及到Swing,只用到了AWT元件,功能很简单,只有+-×/

calculator.java

//////////////////////////////////////////////////////////////////////////////////////////

//计算器功能类,负责具体计算功能的实现
public class calculator
{
 private String result="0";
 private int op=0,add=1,sub=2,mul=3,div=4;/*给加减乘除操作分配int值*/
 
 private double stringToDouble(String x)
 {
  double y=Double.parseDouble(x);
  return y;
 }
 
 private void operate(String x)/*具体的计算操作实现方法*/
 {
  double x1=stringToDouble(x);
  double x2=stringToDouble(result);
  
  switch(op)
  {
   case 0:result=x;break;
   case 1:result=String.valueOf(x2+x1);break;
   case 2:result=String.valueOf(x2-x1);break;
   case 3:result=String.valueOf(x2*x1);break;
   case 4:
   if(x1!=0)result=String.valueOf(x2/x1);
   else {result="The divisor can't be 0!";}break;
  }
 }
 //一个完整的加法运算可以如下描述:a+b=result;先输入a值,然后按"+",随即调用opAdd方法
 //a作为参数传入,计算operate(a);然后修改运算符op为add,返回result=a,后输入第二个
 //数b,后按"=",调用opequals(b)-->operate(b),此次operate方法中op=add,实现加法,返回值
 //result 即为所求,一次运算后把op重新置零
 public String opAdd(String x)/*参数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 String opClean()
 {
  op=0;
  result="0";
  return result;
 }
}

////////////////////////////////////////////////////////////////////////////////////////////

calGUI.java

////////////////////////////////////////////////////////////////////////////////////////////

//计算器GUI类,实现GUI接口与计算功能的连接
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;

public class calGUI
{
 private Frame f;
 private Panel p1,p2;//整个Frame界面用两个panel元件分成两块儿
 private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
 private Button bPoint,bAdd,bSub,bMul,bDiv,bCal;
 private TextField tf;
 private String s,op;
 private calculator cal=new calculator();
 private boolean ifOp;
 
 public calGUI()//构造函数
 {
  f=new Frame("我的计算器");
  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("+");
  bSub=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());//WindowEvent由 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());//"0~9"Button事件由 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()); //各操作符按钮事件由 setOperator_ActionListener类处理
  bSub.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(bDiv);
  p2.add(b4);
  p2.add(b5);
  p2.add(b6);
  p2.add(bMul);
  p2.add(b1);
  p2.add(b2);
  p2.add(b3);
  p2.add(bSub);
  p2.add(b0);
  p2.add(bPoint);
  p2.add(bCal);
  p2.add(bAdd);
  f.add(p2,BorderLayout.SOUTH);
  f.setVisible(true);
 }
 
 public void setTextFieldText_Temp()//文本框内容的改变
 {
  if(tf.getText().length()<15&&(tf.getText().indexOf(".")==-1||!s.equals(".")))
  //这一句的意思是保证参与运算的数小于15位,并且在文本框中已有小数点的情况下不能再次输入小数点
  {
   tf.setText(tf.getText()+s);
  }
  else
  {
   tf.setText((tf.getText()+s).substring(0,15));//substring方法用于取指定位置的字符串
  }
 }
 public void setTextFieldText()
 {
  if(ifOp)  //如果上一次按键是一个运算符,那么先把文本框清空并修改ifOp标记
  {
   ifOp=false;
   tf.setText("");
   setTextFieldText_Temp();
  }
  else
  {
   setTextFieldText_Temp();
  }
 }
 
 public static void main(String argv[])//main方法,创建calGUI类,并完成初始化
 {
  calGUI calculator=new calGUI();
  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();//把按键上显示的字符赋给s
      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;
   }
  }
 }
}

///////////////////////////////////////////////////////////////////////////////////////

 
原创粉丝点击