importjava.awt.*;importjava.awt.event.*;classFirstOne1extendsFrameimplementsActionListener{Framef;ButtonshutDown;FirstOne1(Stringstr){super(str);ButtonshutDown=newButton...
import java.awt.*;
import java.awt.event.*;
class FirstOne1 extends Frame implements ActionListener {
Frame f;
Button shutDown;
FirstOne1(String str) {
super(str);
Button shutDown = new Button("停止");
add(shutDown);
shutDown.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource()==shutDown){
System.out.println("why??"); //此处按按钮后也无法print出来,为什么?
}
}
}
public abstract class Test1 implements ActionListener{
public static void main(String[] args) {
FirstOne1 fo = new FirstOne1("alarm");
fo.setSize(60, 100);
fo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
fo.setVisible(true);
}
}
//为什么无法捕获button的shutDown事件啊?
if去掉是可以,但是如果有多个button又怎么区分呢?
按理说,按下这个按钮是不是应该就是:event.getSource()==shutdown呢?
展开