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. Thtat's why it's saying you didn't implement that method. Use java.awt.event.ActionEvent.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments