0

I'm kind of new to java, so go easy. I'm trying to make a simple game where everytime you click a button it adds one to a variable. That all works fine, but i'm also trying to display the variable to my JFrame. This is where the trouble comes, I click the button, it does add one to my variable (I printed the variable to the console to be sure) but the JFrame isn't updating the variable. I should also note, when you first open the game, it opens a window allowing you to type a username, this is in a separate class, which contains my main method. Here is my code for my second window, the one with the problem:

import javax.swing.BorderFactory;

public class Game extends JFrame {
    private static final long serialVersionUID = 1L;

    private JPanel contentPane;

    private String name;

    public static int pennies = 0;
    public static int dollars = 0;
    public static int moneyAddRate = 1;

    private JButton btnAddMoney = new JButton(new ImageIcon("C:\\Users\\Tanner\\git\\Money-Bags\\res\\coins\\oneCent.png"));

    private Border emptyBorder = BorderFactory.createEmptyBorder();

    public Game(String name) {
        this.name = name;
        createWindow();
    }

    private void createWindow() {
        setTitle(name + "'s Economy");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnAddMoney.setBounds(329, 244, 96, 96);
        btnAddMoney.setBorder(emptyBorder);
        contentPane.add(btnAddMoney);
        btnAddMoney.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                addMoney();
            }
        });

        JLabel lblPennies = new JLabel("You have " + pennies + " Pennies");
        lblPennies.setBounds(10, 11, 152, 24);
        contentPane.add(lblPennies);

        JLabel lblDollars = new JLabel(dollars + " Dollars");
        lblDollars.setBounds(10, 70, 152, 24);
        contentPane.add(lblDollars);

        JLabel lblAnd = new JLabel("&");
        lblAnd.setBounds(10, 45, 61, 14);
        contentPane.add(lblAnd);

        setVisible(true);

    }

    private void addMoney() {
        pennies += moneyAddRate;
        System.out.println(pennies + "  " + dollars);
        contentPane.validate();
        contentPane.repaint();

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tdude179
  • 29
  • 4
  • Where in your `addMoney()` method do you even update a `Component`? You need to set the text of `lblPennies` again using the changed `pennies` amount. – Josh M Aug 17 '13 at 05:32
  • 1) Use code formatting for code, input/output & structured docs like HTML. To do that, select the sample and click the `{}` button above the messaged posting/editing form. 2) 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 Aug 17 '13 at 06:37

1 Answers1

2

It isn't updating because you aren't updating any Component with the new pennies amount. Your addMoney() method should look something like this:

private void addMoney() {
pennies += moneyAddRate;
lblPennies.setText(String.format("You have %d pennies", pennies));
lblPennies.repaint();
}
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • I love you, i've searched through Google all day trying to fix my problem, most of what I found led me to believe the repaint() method would solve all of my problems. Thank you again. – Tdude179 Aug 17 '13 at 05:44