0

I'm trying to make an applet that will count how many times you have clicked a button, but it seems ActionListener isn't working properly. This code looks like it would work, but for some reason when I click the button in the applet, nothing is printed in the console or updated on the interface as it should.

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

public class Test extends Applet implements ActionListener
{
    Button Clicker;
    Button Upgrade;
    int currentClicks = 0;

    public void init()
    {
        this.setSize(600,400);

        Button Clicker = new Button("Click");
        add(Clicker);
        Clicker.addActionListener(this);

        Button Upgrade = new Button("Autoclick Upgrade");
        add(Upgrade);
        Upgrade.addActionListener(this);
    }
    public void paint (Graphics g)
    {
        g.drawString("Test",300,50);
        g.drawString(String.valueOf(currentClicks), 300, 100);
    }
    public void actionPerformed(ActionEvent ae) 
    {
        String sString = ae.getActionCommand();
        if (sString.equals(Clicker))
        {
            System.out.println("Clicker was pressed");
            currentClicks++;
            System.out.println("Total Clicks: "+currentClicks);
            repaint();
        }
        else if (sString.equals(Upgrade))
        {
            System.out.println("Upgrade was pressed");
            if (currentClicks >= 25)
            {
                System.out.println("Upgrade was successfully purchased!");
                currentClicks = currentClicks - 25;
                repaint();
            }
            else
            {
                System.out.println("Upgrade was not successfully purchased!");
                repaint();
            }
        }
    }
}
sparkhee93
  • 1,381
  • 3
  • 21
  • 30
  • 4
    You are comparing a _String_ object with a _Button_ object : _sString.equals(Upgrade)_ – Arnaud Feb 01 '16 at 17:13
  • @Berger I have also tried ae.getSource() == Clicker & ae.getSource() = Upgrade, but that didn't work. How should I compare do this with a button? – Cannon Sloan Feb 01 '16 at 17:16
  • 1
    `ae,getSource() == Clicker` does not work since you never initialize the field `Clicker`! Instead of assigning the button to the field you create a new local variable `Clicker` – Thomas Kläger Feb 01 '16 at 17:26
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 03 '16 at 03:14
  • @AndrewThompson I was just making this in my spare time as a test. I didn't exactly know any other methods of making an application that would run as an applet would. But thanks for letting me know about Swing! That would make things easier. – Cannon Sloan Feb 03 '16 at 16:59

1 Answers1

2

Set an action command to the Button :

Button Clicker = new Button("Click");
Clicker.setActionCommand("Click");

Then use that to determine what was clicked :

if (sString.equals("Click"))

OR

Compare the source button :

if(ae.getSource() == Clicker)
Arnaud
  • 17,229
  • 3
  • 31
  • 44