import java.awt.*;
import java.awt.event.*;
public class calculator extends Frame implements ActionListener,WindowListener{
private TextField text;
private Button button_1,button_2,button_plus,button_cancel,button_amount;
public calculator() {
super("计算器");
this.setSize(320,120);
this.setBackground(java.awt.Color.lightGray);
this.setLocation(300,240);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
text=new TextField(40);
text.setEditable(false);
this.add(text);
button_1=new Button("1");
button_2=new Button("2");
button_plus=new Button("+");
button_cancel=new Button("C");
button_amount=new Button("=");
this.add(button_1);
this.add(button_2);
this.add(button_plus);
this.add(button_cancel);
this.add(button_amount);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_plus.addActionListener(this);
button_cancel.addActionListener(this);
button_amount.addActionListener(this);
this.addWindowListener(this);
this.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button_cancel)
text.setText("");
else if(e.getSource()==button_amount);
{
int i=Integer.parseInt(button_1.getActionCommand());
int j=Integer.parseInt(button_2.getActionCommand());
if (e.getSource()==button_plus)
text.setText(""+(i+j));
//else
//text.setText("*************");
}
else
text.setText(text.getText()+e.getActionCommand());
}
public static void main (String[] args) {
new calculator();
}
}
D:\JAVA语言\java作业\calculator.java:69: 'else' without 'if'
else
^
1 error
处理已完成。
最后还有一个问题:
上面那个源程序相当于一个简单计算器,当我按按钮1的时候,1就显示在文本区中,按按钮2的时候,2就显示在文本区中,现在呢,我按了一个等于按钮,该如何获得按钮1和按钮2中的内容。