简体   繁体   English

Java错误:类'Anonymous'必须被声明为abstract或在'ActionListener'中实现抽象方法'actionPerformed(ActionEvent)'

[英]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.

相关问题 类不是抽象的,并且不会重写ActionListener中的抽象方法actionPerformed(ActionEvent) - Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener 必须将类声明为abstract或实现抽象方法错误 - Class must either be declared abstract or implement abstract method error 类必须声明为抽象或实现抽象方法错误 - Class must be either declared abstract or implement abstract method error RxAndroid:类必须声明为抽象或实现抽象方法错误 - RxAndroid: Class must either be declared abstract or implement abstract method error 类必须声明为abstract或实现抽象方法:Intellij错误? - Class must either be declared abstract or implement abstract method: Intellij error? “必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)”是什么意思? - what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean? 错误: <anonymous m$1> 不是抽象的,并且不会重写ActionListener中的抽象方法actionPerformed(ActionEvent) - error: <anonymous m$1> is not abstract and does not override abstrac method actionPerformed(ActionEvent) in ActionListener 类'从AdListener派生的匿名类'必须被声明为abstract或实现抽象方法'onLoggingImpressionMethod(Ad)' - Class 'Anonymous class derived from AdListener' must either be declared abstract or implement abstract method 'onLoggingImpressionMethod(Ad)' 类&#39;从PlaceSelectedListener派生的匿名类&#39;必须声明为抽象或实现抽象方法 - class 'Anonymous class derived from PlaceSelectedListener' must either be declared abstract or implement abstract method 类必须声明为抽象或实现抽象方法toArray - Class must either be declared abstract or implement abstract method toArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM