I'm just starting Java, and my professor assigned a fairly simple assignment to introduce events and listeners. The program itself is supposed to create two buttons, if you press one a counter goes up. If you press the other the counter goes down. I have my code in three parts.
First the main static void:
//ButtonCounter.java
import javax.swing.JFrame;
public class ButtonCounter
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Button Counter");
ButtonCounterPanel panel = new ButtonCounterPanel();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
-----------------------------------
Then another program called ButtonCounterPanel.java
// ButtonCounterPanel.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonCounterPanel extends JPanel
{
private int count;
private JLabel label1;
private JButton push1, push2;
public ButtonCounterPanel()
{
count = 0;
label1 = new JLabel("Pushes: " + count);
push1 = new JButton("Push Me!");
push2 = new JButton("No, Me!");
ButtonListener listener = new ButtonListener(this);
push1.addActionListener(listener);
push2.addActionListener(listener);
add(push1);
add(push2);
setBackground(Color.cyan);
setPreferredSize(new Dimension(350, 60));
}
// creates the methods for the listeners to implement
public void incrementCount()
{
count++;
label1.setText("Pushes: " + count);
}
public void decrementCount()
{
count--;
label1.setText("Pushes: " + count);
}
}
----------------------------------------
Finally the listener code: ButtonListener.java
//ButtonListener.java
// the push button listener for ButtonCounterPanel
import java.awt.event.*;
public class ButtonListener implements ActionListener
{
private ButtonCounterPanel panel;
public ButtonListener(ButtonCounterPanel pushPanel)
{
panel = pushPanel;
}
public void push1actionPerformed(ActionEvent event)
{
panel.incrementCount;
}
public void push2actionPerformed(ActionEvent event)
{
panel.decrementCount;
}
}
------------------------------------------
I'm getting two different errors (really three, but the last two are the same thing.)
The first error is:
.\ButtonListener.java:6: ButtonListener is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class ButtonListener implements ActionListener
this is the one that is throwing me off the most. What does it mean that the listener is not abstract?
The other error is:
.\ButtonListener.java:17: not a statement
panel.incrementCount;
Any suggestions on this would be greatly appreciated.
-Alexis