刚才说到哪了。。。恩恩编辑一个计算器程序:
import java.awt.*;
import java.awt.event.*;
public class CalculatorFrame extends Frame implements ActionListener {
private TextField text;
private Button button_1,button_2,button_plus,button_cancel;
public CalculatorFrame(){
super("calculator");
this.setSize(320, 120);
this.setBackground(Color.lightGray);
this.setLocation(300, 240);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
text=new TextField(40);
text.setEditable(false);
this.add(text);
button_1=new Button("1");
this.add(new Button("1"));
button_2=new Button("2");
this.add(new Button("2"));
button_plus=new Button("+");
this.add(new Button("+"));
button_cancel=new Button("C");
this.add(new Button("C"));
this.addWindowListener(new WinClose());//为框架注册窗口事件监听器 委托winclose类的对象处理事件
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){//calcuatorframe类继承了actionlistener接口 就必须实现这个接口的全部方法 接口ActionListener中只有一个public void actionPerformed(ActionEvent e)方法
if(e.getSource()==button_cancel);
text.setText("");
text.setText(text.getText()+e.getActionCommand());
}
public static void main(String arg[]){
new CalculatorFrame();
}
}
class WinClose implements WindowListener{
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
可是结果有点小问题啊...等我回家再看好了。。。先回家了~~吼吼~~