java小程序的错误

来源:百度知道 编辑:UC知道 时间:2024/05/31 18:44:30
错误是:Monitor不是抽象的,并未覆盖ActionListener中抽象方法ActionEvent
import java.awt.*;
import java.awt.event.*;
public class ActionEvent
{
public static void main(String args[])
{
Frame frame=new Frame("Action");
Button button=new Button("Press me");
Monitor b=new Monitor();
button.addActionListener(b);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

}

}
class Monitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.print("You have press the button");
}
}
我把主类名改了改成TestActionEvent可还是出同样的问题,新手刚刚开始学的而且是自学的,所以写法习惯什么的没注意,谢谢提醒,改那的代码改后可以运行成功,说的具体点,谢谢

如同一楼所说,你主类的名字是ActionEvent,他会认为ActionEvent e是那个的类的对象.
LZ的写法习惯很不好,出错的地方这样写,其他地方Frame frame=new Frame("Action"); Button button=new Button("Press me");
都是用了和类名一样的.这种习惯不好.

你覆盖的方法所传的参数的类型有问题
public void actionPerformed(ActionEvent e)
ActionEvent 跟你上面写的类名一样了,所以你要改成
java.awt.event.ActionEvent这样就可以编译通过了

这程序写的,啧啧……

在类命名的时候不要使用和java类库中相同的名字!