Java error: class "Anonymous" must either be declared abstract or implement the abstract method "actionPerformed (ActionEvent)" in "ActionListener"

I'm trying to set up an action listener for a button and I can't figure out why. I have mentioned a lot of tutorials but I get that it must be declared abstract or must implement an abstract method ..... error. I've seen similar threads about fixing this, but nothing that really helped me. Any help would be great. Here's a quick example 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();
    }
}

      

+3


source to share


3 answers


You imported the wrong ActionEvent class. That's why he said you didn't implement this method. Use java.awt.event.ActionEvent.



+5


source


use import java.awt.event.ActionListener instead of import javafx.event.ActionEvent



+4


source


Import

java.awt.event.ActionEvent

      

instead

javafx.event.ActionEvent

      

+4


source







All Articles