Trevn Jones
Trevn Jones

Reputation: 333

If Statements aren't working properly

I'm just messing around with buttons on Java and am trying to just simply create a very simple stupid version of "Cookie Clicker" However, the if statements in the listener class aren't working like they should. Main Class Code:

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class CookieClickerGame {
    private static final int FRAME_WIDTH = 200;
    private static final int FRAME_HEIGHT = 200;

    public static void main(String[] args) {
        JPanel panel = new JPanel();

        JFrame frame = new JFrame();
        JButton cookie = new JButton("Cookie");
        JButton grandma = new JButton("Grandma");
        panel.add(cookie);
        panel.add(grandma);
        frame.add(panel);

        ActionListener listener = new CookieClicker();
        cookie.addActionListener(listener);

        ActionListener listener2 = new CookieClicker();
        grandma.addActionListener(listener2);

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Listener Class Code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class CookieClicker implements ActionListener {
    int cookies;
    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("Cookie")) {
            cookies++;
            System.out.println(cookies + " Cookies");
        } else if (event.getActionCommand().equals("Grandma") && cookies >= 10) {
            System.out.println("+1 Grandma");
        } else {
            JOptionPane.showMessageDialog (null, "Not Enough Cookies", "You're Poor", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

My problem is with the Grandma part, Nothing ever shows up saying "+1 Grandma" and even if I have enough cookies it will still tell me I'm too poor. What's wrong with the code?

Upvotes: 1

Views: 137

Answers (4)

Markus Patt
Markus Patt

Reputation: 477

ActionListener listener = new CookieClicker();
cookie.addActionListener(listener);

ActionListener listener2 = new CookieClicker();
grandma.addActionListener(listener2);

You are creating two different listeners for your buttons.

listener is notified when the first button is clicked and listener2 is notified when the second button is clicked.

Just use the same listener for both buttons:

ActionListener listener = new CookieClicker();
cookie.addActionListener(listener);

grandma.addActionListener(listener);

Upvotes: 0

Ahmed Sayed
Ahmed Sayed

Reputation: 466

change the cookies counter to static:

static int cookies;

Upvotes: 1

Yassin Hajaj
Yassin Hajaj

Reputation: 22005

Change :

int cookies;

to :

static int cookies;

So that you have one and only cookies field for all your objects and it changes each time your action is performed. Either with the first or second button.

Upvotes: 1

Paul Hicks
Paul Hicks

Reputation: 14019

You have two different CookieClicker instances. Either use a static member or have listener's action update grandma's cookie value.

If you use the latter option, you might has well have two separate classes, CookieClicker and GrandmaClicker. Then CookieClicker.actionPerformed() can call GrandmaClicker.IncrementCookies() or similar.

Upvotes: 3

Related Questions