帮我看看怎么改啊
package lesson4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator {
private JFrame frame;
private Container contentPane;
private JPanel centerPanel;
private JTextField resultTxt;
private DigitalButton[] digBtn = new DigitalButton[10];
private OperatorButton[] operBtn = new OperatorButton[6];
private static double a=0;
private static double b=0;
private boolean tab=true;
public Calculator() {
// 1 choose a container
frame = new JFrame("Calculator");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(250,300);
contentPane = frame.getContentPane();
initGUI();
}
private void initGUI() {
// 2 choose a layout manager
contentPane.setLayout(new BorderLayout());
// 3 add component
resultTxt = new JTextField(80);
contentPane.add(resultTxt, BorderLayout.NORTH);
// 3.1 choose a container
centerPanel = new JPanel();
// 3.2 choose a layout manager;
centerPanel.setLayout(new GridLayout(4, 4));
resultTxt.setText("0.0");
// 3.3 add component
for (int i = 0; i <= 9; i++) {
digBtn[i] = new DigitalButton(i);
// 3.4 add eventHandler
digBtn[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DigitalButton db = (DigitalButton) e.getSource();
double d1 = db.getValue();
if(tab==true)
{a=a*10+d1;resultTxt.setText(Double.toString(a));
System.out.println("a:"+a);
}
else
{b=b*10+d1;resultTxt.setText(Double.toString(b));
System.out.println("b:"+b);
}
}
});
centerPanel.add(digBtn[i]);
}
operBtn[0] = new OperatorButton("+");
operBtn[1] = new OperatorButton("-");
operBtn[2] = new OperatorButton("*");
operBtn[3] = new OperatorButton("/");
operBtn[4] = new OperatorButton("=");
operBtn[5] = new OperatorButton("C");
for (int i = 0; i <= 5; i++) {
// 3.4 add event Handler
operBtn[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OperatorButton db = (OperatorButton) e.getSource();
String c=db.getOperator();
if(c.equals("C")){
a=0;b=0;
resultTxt.setText("0.0");
tab=true;
}
if(c.equals("+"))
{
tab=false;
a+=b; //怎么让b赋值后再来加啊!
}
else if(c.equals("-"))
{
tab=false;
a-=b;
}
else if(c.equals("/"))
{
tab=false;
a/=b;
}
else if(c.equals("*"))
{
tab=false;
a*=b;
}
else
{
tab=true;
System.out.println("=:"+a);
resultTxt.setText(Double.toString(a));
a=0;b=0;
}
}
});
centerPanel.add(operBtn[i]);
}
contentPane.add(centerPanel, BorderLayout.CENTER);
}
public void go() {
frame.setVisible(true);
}
public static void main(String[] args) {
(new Calculator()).go();
}
}
class DigitalButton extends JButton {
private double value;
public double getValue() {
return value;
}
public DigitalButton(int value) {
super(value + "");
this.value = value;
}
}
class OperatorButton extends JButton {
private String operator;
public String getOperator() {
return operator;
}
public OperatorButton(String operator) {
super(operator);
this.operator = operator;
}
}