- Java code
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 Calculator extends WindowAdapter implements ActionListener{ private double result = 0; private double data1 = 0; private boolean resultIndicate = false; private Frame f; private Button b1, b2, b3, b4, b5, b6, b7, b8, b9; private TextField t; private Panel p,p1,p2; private char prec='+'; private int zhi=0; private int nextzhi=0; private int sum=0; static public void main(String args[]) { Calculator cc = new Calculator(); cc.go(); } public void go() { f = new Frame("Calculator"); p = new Panel(); p1 = new Panel(); p2 = new Panel(); p.setLayout(new GridLayout(3,3)); p2.setLayout(new GridLayout(1, 1)); p1.setLayout(new GridLayout(2, 1)); 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"); t = new TextField("0",30); f.add(t,"South"); f.add(p,"Center"); f.add(p1,"East"); f.add(p2,"North"); p.add(b1); p.add(b2); p.add(b3); p.add(b4); p.add(b5); p.add(b6); p.add(b7); p.add(b8); p.add(b9); 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); f.setSize(250, 190); f.setVisible(true); } public void actionPerformed(ActionEvent es) { // TODO Auto-generated method stub if(es.getSource()==b1){t.setText(t.getText()+1);} if(es.getSource()==b2){t.setText(t.getText()+2);} if(es.getSource()==b3){t.setText(t.getText()+3);} if(es.getSource()==b4){t.setText(t.getText()+4);} if(es.getSource()==b5){t.setText(t.getText()+5);} if(es.getSource()==b6){t.setText(t.getText()+6);} if(es.getSource()==b7){t.setText(t.getText()+7);} if(es.getSource()==b8){t.setText(t.getText()+8);} if(es.getSource()==b9){t.setText(t.getText()+9);} zhi=Integer.parseInt(t.getText()); StringBuffer sb=new StringBuffer(t.getText()); int length=sb.length(); sb.deleteCharAt(length-1); t.setText(sb.toString()); nextzhi=Integer.parseInt(t.getText()); sum=zhi+nextzhi; t.setText(sum+""); } public void windowClosing(WindowEvent e) { System.exit(0); }}
这是我写的另一个加法计算器的代码。
这个的原理是按下界面上的数字,在最底下的文本框显示相应的数字并累加,同时消除前一个数字。
程序能运行,但有三个问题感到很奇怪:
1、为什么程序点右上角的叉叉无法正常退出,只有退出eclipse后才能退出?
2、程序最大只能计算到20,超过20之后就不正常了;
3、第二次输入数字之后,前一个数字不会消失。比如输入1后,文本框显示1;当输入2后,应该显示的是3,可是我这里显示的是13。
请问各位大大如何修改?
谢谢!
------解决方案--------------------
楼主这个方法actionPerformed写的有点乱,就是两个数相加的问题,int c=a+b;肿么写的那么繁琐。不知道你想要的是什么效果。我改了这个方法
public void actionPerformed(ActionEvent es) {
//相加之前的结果值
int befAdd = Integer.parseInt(t.getText());
//本轮需要相加的值
int addVal = 0;
// TODO Auto-generated method stub
if(es.getSource()==b1){t.setText(t.getText()+1);addVal = 1;}