Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

First Program with Events

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

Comments

  • themikestthemikest Member Posts: 1
    Hi,

    When you implement the ActionListener, you should override the actionPerformed(java.awt.event.ActionEvent) method, something like this:
    @Override
    public void actionPerformed(ActionEvent event) {
    //your code
    }
    Your concept of push1actionPerformed(ActionEvent event) and push2actionPerformed(ActionEvent event) methods are wrong. It seems that you do not understand the basics of Java.

    The other: "panel.incrementCount;" looks like a method call. But methods have brackets after their name like this:
    "panel.incrementCount();"
    I suggest you read a basic Java tutorial.

    Best regards,
    Mike
  • silveredge52silveredge52 Member Posts: 198
    Hey,
    themikest is right. The ActionListner class is abstract and to implement that class its abstract method(s), actionPerformed(), must be implemented.

    Attached is beginning code that show how to accomplish objective.

    regards, silveredge52.
  • AlexisofrosesAlexisofroses Member Posts: 2
    Thanks for the help guys, especially pointing me to the java tutorials. My professor doesn't really teach anything, he just throws up a whole bunch of code and sort of expects us to absorb and comprehend it. It's really quite frustrating. Anyways, the tutorials helped a ton, and also helped me program the project more efficiently. This is my final solution, which compiles correctly, and does what it is supposed to:

    import java.awt.*;
    import java.awt.event.*;

    public class AL extends Frame implements WindowListener,ActionListener {
    TextField text = new TextField(20);
    Button b, b2;
    private int numClicks = 0;

    public static void main(String[] args) {
    AL myWindow = new AL("My first window");
    myWindow.setSize(350,100);
    myWindow.setVisible(true);
    }

    public AL(String title) {

    super(title);
    setLayout(new FlowLayout());
    addWindowListener(this);

    b = new Button("Click me");
    add(b);
    add(text);
    b.addActionListener(this);

    b2 = new Button("Click me too");
    add(b2);
    add(text);
    b2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {


    if(e.getSource() == b)
    {
    numClicks++;
    text.setText("Button Clicked " + numClicks + " times");
    }

    else
    {
    numClicks--;
    text.setText("Button Clicked " + numClicks + " times");
    }
    }



    public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
    }

    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}

    }




    Thanks again for all the help.

    -Alexis
Sign In or Register to comment.