简体   繁体   中英

Trouble updating my JFrame

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();

    }
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM