![](/img/trans.png)
[英]Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
[英]Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'
我一直在努力为按钮设置一个动作监听器,我无法弄清楚为什么。 我已经提到了许多教程,但我已经得到必须声明为抽象或必须实现抽象方法.....错误。 我已经看过类似的关于解决这个问题的话题,但没有什么能真正帮助我。 任何帮助都会很棒。 这是一个与我正在做的类似的简短示例:
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;
import javax.swing.*;
public class Kitty {
private static void mainFrame() {
JFrame mainFrame = new JFrame("Kitty");
JPanel mainPanel = new JPanel();
mainFrame.setSize(200,200);
mainFrame.setResizable(false);
mainFrame.add(mainPanel);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
JButton button1 = new JButton("Pet the kitty");
mainPanel.add(button1);
button1.addActionListener(new ActionListener(){
// Line above (Specifically ActionListener) says Class 'Anonymous' must either be declared abstract or
// implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'
public void actionPerformed(ActionEvent event){
System.out.println("Purrrrrr....");
}
});
}
public static void main(String[] args) {
mainFrame();
}
}
您导入了错误的ActionEvent类。 这就是为什么它说你没有实现那种方法。 使用java.awt.event.ActionEvent。
使用import java.awt.event.ActionListener而不是import javafx.event.ActionEvent
进口
java.awt.event.ActionEvent
代替
javafx.event.ActionEvent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.