计算器

import java.awt.*;
import java.awt.event.*;
public class Exe17_15_1 extends Frame implements ActionListener
{
int result;
String n;
TextField tf;
String n1,n2,operator,operator2;
Button bb=new Button("Backspace");
Button ce=new Button("CE");
Button c=new Button("C");
public e(String title){
super(title);

tf=new TextField();
tf.setText(null);
Panel p1=new Panel();
p1.setLayout(new GridLayout(1,3));
Panel p2=new Panel();
p1.add(bb); p1.add(ce); p1.add(c);
p2.setLayout(new GridLayout(4,4));

Button[] b=new Button[10];
b[0]=new Button("0");
b[1]=new Button("1");
b[2]=new Button("2");
b[3]=new Button("3");
b[4]=new Button("4");
b[5]=new Button("5");
b[6]=new Button("6");
b[7]=new Button("7");
b[8]=new Button("8");
b[9]=new Button("9");//定义数字按钮


Button[] op=new Button[6];
op[0]=new Button("+");
op[1]=new Button("-");
op[2]=new Button("*");
op[3]=new Button("/");
op[4]=new Button(".");
op[5]=new Button("=");//定义符号按钮

for(int i=0;i<b.length;i++){
b[i].addActionListener(this);
p2.add(b[i]);
}//按钮注册监听事件
for(int i=0;i<op.length;i++){
op[i].addActionListener(this);
p2.add(op[i]);
}//符号注册监听事件
ce.addActionListener(this);
c.addActionListener(this);bb.addActionListener(this);//清除等四个键注册
add(tf,BorderLayout.NORTH);add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);//将组件添加到本框架中

}//构造函数
public void actionPerformed(ActionEvent e){
n=e.getActionCommand();
if(n.equals("1")||n.equals("2")||n.equals("3")||n.equals("4")||n.equals("5")||
n.equals("6")||n.equals("7")||n.equals("8")||n.equals("9")){
if(n1==null&&n2==null){ n1=n;tf.setText(n); }
else if(n2==null&&operator==null&&n1!=null){n1=tf.getText()+n;tf.setText(n1);}
else if(operator!=null){
if(n2==null){n2=n;tf.setText(n); }
else if(n2!=null&&operator2==null){n2=tf.getText()+n;tf.setText(n2);}
}

}//if
else if(n.equals("+")||n.equals("-")||n.equals("*")||n.equals("/"))operator=n;

else if(n.equals("."))tf.setText(tf.getText()+".");
else if(n.equals("="))operator2=n;
else if(n.equals("Backspace")){
String s=tf.getText();
tf.setText(s.substring(0,s.length()-1));
if(operator==null)n1=tf.getText();
if(n1!=null&&operator!=null&&operator2==null)n2=tf.getText();

}//清除一位
else if(n.equals("CE")){tf.setText("0");n1=null;n2=null;operator=null;operator2=null;}
else if(n.equals("C")){tf.setText(null);n1=null;n2=null;operator=null;operator2=null;}

if(n1!=null&&n2!=null&&operator!=null&&operator2!=null&&!n.equals("CE")&&!n.equals("C")){
if(operator.equals("+")){
result=Integer.parseInt(n1)+Integer.parseInt(n2);

}
if(operator.equals("-")){
result=Integer.parseInt(n1)-Integer.parseInt(n2);

}
if(operator.equals("*")){
result=Integer.parseInt(n1)*Integer.parseInt(n2);

}

if(operator.equals("/")){
result=Integer.parseInt(n1)/Integer.parseInt(n2);

}
tf.setText(String.valueOf(result));
n1=null;n2=null;operator=null;operator2=null;
}//if


}//actionPerformed
public static void main(String[] args) {
Exe17_15_1 f=new Exe17_15_1("计算器");
f.setLocation(300,300);
f.setSize(300,300);
f.setVisible(true);
f.pack();


}

}>

原文地址:https://www.cnblogs.com/lan0725/p/1873995.html