62,614
社区成员
发帖
与我相关
我的任务
分享
//把这段代码加到构造函数里面
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
import java.awt.*;
import java.awt.event.*;
public class EventTest implements ActionListener{
Frame f;
Button b;
public EventTest(){
f = new Frame("testing");
b = new Button("press");
b.addActionListener(this);
f.add(b,"North");
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//实现接口中所有的方法
public void actionPerformed(ActionEvent e){
b.setLabel("I have been pressed");
}
public static void main(String[] args)throws Exception{
EventTest et = new EventTest();
}
}