0

so thanks for looking at my question,Well anyways im trying to make a cookie clicker clone (if you could call it that) where you click the button and the JPanel updates, unfortunately the JPanel doesn't update and I just cant find an answer anywhere else, Any help is valuable help, thank you! Here is my code:

    public int numCookies;

public main(){

    //JButton
    JButton click = new JButton("Click");
    click.setLayout(null);
    click.setLocation(50, 50);
    click.setSize(80, 50);
    click.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0){
            numCookies = numCookies + 1;
        }
    });

//JLabel
JLabel cookies = new JLabel();
cookies.setLayout(null);
cookies.setText("Cookies:" + numCookies);
cookies.setLocation(480/2,10);
cookies.setSize(200, 50);

    //JPanel
JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setLocation(10, 0);
    panel.setSize(260, 30);
    panel.add(click);
    panel.add(cookies);

    //JFrame
    JFrame frame = new JFrame("Cookie clicker!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 480);
    frame.setLocationRelativeTo(null);
    pack();
    frame.setVisible(true);
    frame.add(panel);

}

public static void main(String args[]){

    new main();

}
Puzzelism
  • 1
  • 1
  • I see `setLayout(null);` and aren't surprised you're having issues...use an appropriate [layout manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), no execuses – MadProgrammer Mar 14 '14 at 05:06
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 14 '14 at 05:13

2 Answers2

1

Three things...

One...

Use of null layouts. Don't be surprised when things go wrong. When using JLabel or JButton, unless you intend to actually add something to them, you don't need to touch there layout managers.

Make use of approriate layout managers

Two...

Calling setVisible on your frame before you've finished filling it out...

JFrame frame = new JFrame("Cookie clicker!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 480);
frame.setLocationRelativeTo(null);
// No idea what you expect this to be doing...
pack();
//frame.setVisible(true);
frame.add(panel);    
// This belongs here...and you shouldn't pack a window until
// it's ready to be made visible...
frame.setVisible(true);

Three...

You seem to expect that the lable will magically now that the value numCookies has changed...

click.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0){
        numCookies = numCookies + 1;
        // ??????
    }

You need to reassign the value to the label in order for the label to be updated...

For example...

public main(){
    //JLabel
    // Make the lable final...so we can access from witin
    // the listener...
    final JLabel cookies = new JLabel();
    cookies.setText("Cookies:" + numCookies);

    //JButton
    JButton click = new JButton("Click");
    click.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0){
            numCookies = numCookies + 1;
            cookies.setText("Cookies:" + numCookies);
        }
    });

Bonus

Make sure you read through and understand Initial Threads

...And just because null layouts annoy me so much...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public int numCookies;

    public Main() {

        final JLabel cookies = new JLabel();
        cookies.setText("Cookies:" + numCookies);

        JButton click = new JButton("Click");
        click.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                cookies.setText("Cookies:" + (++numCookies));
            }
        });

        //JFrame
        JFrame frame = new JFrame("Cookie clicker!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        frame.add(cookies, gbc);
        frame.add(click, gbc);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                new Main();
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Set the text of the JLabel in the actionPerformed method like this:

click.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0){
        numCookies = numCookies + 1;
        cookies.setText("Cookies:" + numCookies);
    }
});
Noob
  • 534
  • 1
  • 8
  • 16