Java错误:“匿名”类必须声明为抽象,或者必须在“ ActionListener”中实现抽象方法“ actionPerformed(ActionEvent)”

TheCorey28

我一直在努力为按钮设置动作侦听器,但我不知道为什么。我已经提到过许多教程,但是我一直在获得必须声明为抽象或必须实现抽象方法的错误。我已经看到了有关解决此问题的类似主题,但没有任何真正帮助我的问题。任何帮助将是巨大的。这是一个简短的示例,与我正在执行的操作类似:

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();
    }
}
wvdz

您导入了错误的ActionEvent类。这就是为什么您没有实现该方法的原因。使用java.awt.event.ActionEvent。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档