oopsidoodles
oopsidoodles

Reputation: 17

Button not doing anything?

So I would consider myself to be a beginner programmer, but something as simple as getting a button to do something once clicked is fairly easy, right? I'll paste the code first then ask the question.

    /*
 * Created By Vili Milner
 */

import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class MainView extends JFrame implements ActionListener {

private long cookieBalance = 0;
private String stringBalance = Long.toString(cookieBalance);
private JLabel balance = new JLabel(stringBalance);

public MainView(){
    display();
}

public void display(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setTitle("Cookie Clicker");
    setSize(300, 200);

    GridLayout gridOneTwo = new GridLayout(1, 2);
    GridLayout gridOneTen = new GridLayout(1, 10);
    GridLayout gridOneThree = new GridLayout(1, 3);

    Panel mainPanel = new Panel();
    mainPanel.setLayout(gridOneTwo);

    Panel upgradePanel = new Panel();
    upgradePanel.setLayout(new BoxLayout(upgradePanel, BoxLayout.Y_AXIS));
    JButton upgradeButtonOne = new JButton("UPGRADE 1");
    JButton upgradeButtonTwo = new JButton("UPGRADE 2");
    JButton upgradeButtonThree = new JButton("UPGRADE 3");
    JButton upgradeButtonFour = new JButton("UPGRADE 4");
    JButton upgradeButtonFive = new JButton("UPGRADE 5");
    upgradePanel.add(upgradeButtonOne);
    upgradePanel.add(upgradeButtonTwo);
    upgradePanel.add(upgradeButtonThree);
    upgradePanel.add(upgradeButtonFour);
    upgradePanel.add(upgradeButtonFive);
    mainPanel.add(upgradePanel);

    Panel displayPanel = new Panel();
    displayPanel.setLayout(new BoxLayout(displayPanel, BoxLayout.Y_AXIS));
    displayPanel.add(balance);
    mainPanel.add(displayPanel);

    Panel cookiePanel = new Panel();
    cookiePanel.setLayout(gridOneThree);
    JButton cookieButton = new JButton("COOKIE");
    cookieButton.setActionCommand("cookie");
    cookieButton.addActionListener(this);
    cookiePanel.add(cookieButton);
    JLabel emptyLabelOne = new JLabel(" ");
    JLabel emptyLabelTwo = new JLabel(" ");
    displayPanel.add(cookiePanel);
    displayPanel.add(emptyLabelOne);
    displayPanel.add(emptyLabelTwo);

    add(mainPanel);
}

@Override
public void actionPerformed(ActionEvent click) {
    String action = click.getActionCommand();

    if (action.equals("cookie")){
        cookieBalance++;
    }
}

}

Continuing, I can get the button to do anything except make the display label go up by 1. In other words, the button itself does work, but for some reason the label isn't changing. I believe this is a fairly simple mistake, I just can't seem to find it. So my question is: why is the label not changing once I increment the value?

Upvotes: 1

Views: 97

Answers (1)

coderatchet
coderatchet

Reputation: 8420

Repeating what has been said in the comments, The JLabel balance is not being updated with the value, It simply reflects the value of what cookieBalance originally was. Rather than simply incrementing the variable, you should call:

@Override
public void actionPerformed(ActionEvent click) {
    String action = click.getActionCommand();

    if (action.equals("cookie")){
        balance.setText(String.valueOf(++cookieBalance));
    }
}

Upvotes: 1

Related Questions