Im trying to get a simple GUI working.
Firstly here is a brief description of the program:
Its just a small JFrame, with 1 button and an actionListener for when the button is pressed. The number of 'pushes' is updated and printed when the button is pressed.
I have a main class: PushCounter and another class: PushCounterPanel for which all the classes and methods are written for the JFrame
Heres the code:
package Chapter4; // demonstrates a GUI and an event listener import javax.swing.JFrame; public class PushCounter { // creates the main program frame public static void main(String[] args) { JFrame frame = new JFrame("Push Counter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new PushCounterPanel()); frame.pack(); frame.setVisible(true); } }
package Chapter4; import java.awt.*; import java.awt.event.*; import javax.swing.*; // demonstrates a GUI and an event listener public class PushCounterPanel extends JPanel { private int count; private JButton push; private JLabel label; // constructor: sets up the GUI public PushCounterPanel() { count = 0; push = new JButton("Push me!"); push.addActionListener(new ButtonListener()); label = new JLabel("Pushes: " + count); add(push); add(label); setPreferredSize(new Dimension(300, 40)); setBackground(Color.cyan); } // represents a listener for button push (action) events private class ButtonListener implements ActionListener { // updates the counter and label when the button is pushed: public void actionperformed(ActionEvent event) { count++; label.setText("Pushes: " + count); } } }
And the error:
PushCounterPanel.ButtonListener is not abstract and does not override abstract method action performed (java.awt.ActionEvent) in java.awt.event.ActionListener
Its on line 33 (this line) :
private class ButtonListener implements ActionListener {
In which ButtonListener is red underlined
Any input appreciated