[英]Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'
I've been struggling to setup an action listener for a button, and I can't figure out why. 我一直在努力为按钮设置一个动作监听器,我无法弄清楚为什么。 I've referred to many tutorials, but I've been getting the must be declared abstract or must implement abstract method ..... error.
我已经提到了许多教程,但我已经得到必须声明为抽象或必须实现抽象方法.....错误。 I've seen similar topics about fixing this, but nothing that really helped me out.
我已经看过类似的关于解决这个问题的话题,但没有什么能真正帮助我。 Any help would be great.
任何帮助都会很棒。 Here is a short example that is similar to what I am doing:
这是一个与我正在做的类似的简短示例:
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();
}
}
You imported the wrong ActionEvent class. 您导入了错误的ActionEvent类。 Thtat's why it's saying you didn't implement that method.
这就是为什么它说你没有实现那种方法。 Use java.awt.event.ActionEvent.
使用java.awt.event.ActionEvent。
使用import java.awt.event.ActionListener而不是import javafx.event.ActionEvent
Import 进口
java.awt.event.ActionEvent
instead of 代替
javafx.event.ActionEvent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.