我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Events1 extends JFrame {
private JLabel label;
private JButton button;
public Events1() {
setLayout(new FlowLayout());
label = new JLabel("");
button = new JButton("Click for text");
add(button);
add(label);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerfomed(ActionEvent e) {
label.setText("See motherfucker it does do stuff");
}
}
public static void main(String[] args) {
Events1 window = new Events1();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 500); //.pack();
window.setVisible(true);
window.setTitle("Attempt 2");
}
}
基本上,我是 GUI 新手,当我尝试编译上述代码时收到错误消息:
Events1.java:25: error: Events1.event is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class event implements ActionListener {
^
1 error
我基本上是根据 Oracle 文档上的信息编写了这段代码,并且对为什么它不起作用/如何修复它感到非常困惑。
非常感谢任何帮助,谢谢。
最佳答案
您的重写方法中有一个拼写错误
public void actionPerformed(ActionEvent e)
这就是为什么您应该对重写方法使用 @Override 注解,并为此类操作使用 IDE 支持。
关于java - 涉及 actionListener 的类不是抽象的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846922/