import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Applet1 extends Frame implements ActionListener {
Button bt1,bt2;
Label 11;
Applet1(){
super( "Window1 ");
addWindowListener(this);
setSize(400,200);
bt1=new Button( "111 ");
bt2=new Button( "222 ");
11=new Label( "点击任意按钮 ");
setLayout(new FlowLayout());
add(11);
add(bt1);
add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
show();
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowOpen(WindowEvent e);
public void windowClose(WindowEvent e);
public void windowIconified(WindowEvent e);
public void windowDeiconified(WindowEvent e);
public void windowActivate(WindowEvent e);
public void windowDeactivate(WindowEvent e);
public static void main(String args[]){
new Applet1();
}
public void actionPerformed(ActiveEvent e){
11.setText( "你按下的是 "+e.getActionCommand()+ " ");
}
}
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Test extends Frame implements ActionListener {
Button bt1,bt2;
Label l11;
Test(){
super( "Window1 ");
setSize(400,200);
bt1=new Button( "111 ");
bt2=new Button( "222 ");
l11=new Label( "点击任意按钮 ");
setLayout(new FlowLayout());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
add(l11);
add(bt1);
add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
show();
}
public static void main(String args[]){
new Test();
}
public void actionPerformed(ActionEvent e) {
l11.setText( "你按下的是 "+e.getActionCommand()+ " ");
}
一、利用适配器WindowAdapter,他实现了接口中所有方法,但是方法体是空的,所以你只要覆盖你需要覆盖的方法就可以了
二、不要用数字做变量名
三、最后public void actionPerformed(ActionEvent e)中,是ActionEvent不是ActiveEvent,你把我累死了,我用IDE工具去编辑才发现这个问题