让我郁闷两天的问题
sob 2004-02-17 03:26:05 我是新手,写了个小程序来处理按钮单击事件,如下:
import java.awt.*;
import java.awt.event.*;
public class TestButton{
public static void main (String args[]){
//private Frame f;
//private Button b;
Frame f = new Frame("Test");
Button b = new Button("Press me");
b.addActionListener(new ButtonHandler());
f.add(b,"Center");
f.pack();
f.setVisible(true);
//b.addActionListener(new ButtonHandler());
}
public class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Action occurred");
System.out.println("Button's label is : " + e.getActionCommand());
}
}
}
用javac编译后出现如下错误:
non-static varible this cann't be referenced from a static context
b.addActionListener(new ButtonHandler())
^
我知道是new ButtonHandler()出错,但不知错在哪,我明明定义了ActionListener接口的实现类啊?
Any Help Would be Greatful!一定给分!!