简体   繁体   English

使用静态整数更新JLabel

[英]Updating a JLabel with a Static Integer

So I'm new to Java, I took a class in highschool last year and want to try and make my own little 2d game that Im working on. 因此,我是Java的新手,去年我在高中上了一堂课,想尝试制作自己的小2d游戏,我正在研究这个游戏。 I have a stats.java that is filled with all the variables I want stored, such as cash, name, level, etc. Right now Im trying to add Cash to my Cash JLabel using a button. 我有一个stats.java,其中充满了我要存储的所有变量,例如现金,名称,级别等。现在,我正在尝试使用按钮将Cash添加到我的Cash JLabel。

JButton btnAddCash = new JButton("ADD 10,000");
btnAddCash.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        stats.cash = (stats.cash + 5000);
    }
});

JLabel lblCash = new JLabel("Cash: " +stats.cash);
lblCash.setForeground(Color.WHITE);
lblCash.setBounds(10, 649, 162, 14);
contentPane.add(lblCash);
lblCash.setFont(new Font("AirbusMCDUa", Font.BOLD, 15));
JButton debugBtn = new JButton("");

Any help would be awesome! 任何帮助都是极好的!

Your problem is here at (A) and (B) 您的问题在(A)和(B)

JButton btnAddCash = new JButton("ADD 10,000");
btnAddCash.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        stats.cash = (stats.cash + 5000); //     (A)
    }
});

JLabel lblCash = new JLabel("Cash: " +stats.cash); //     (B)

Understand that when you create the JLabel it holds the present value of the cash field and does not hold a reference to the field itself. 请理解,当您创建JLabel时,它会保留现金字段现值,而不是对该字段本身的引用。 This is important since updating the cash field will have no effect on the JLabel's text. 这很重要,因为更新现金字段将不会影响JLabel的文本。 You must explicitly change that text: 您必须显式更改该文本:

btnAddCash.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        stats.cash = (stats.cash + 5000);    
        lblCash.setText("Cash: " +stats.cash);  // ****** update the text
    }
});

Other issues: 其他事宜:

  • Again, avoid static unless you have a good reason for using this 同样,除非有充分的理由使用它,否则请避免使用static
  • Look up the Model-View-Controller design pattern and study it. 查找Model-View-Controller设计模式并进行研究。 This type of structure is what you will eventually want to use. 最终将要使用这种类型的结构。
  • Avoid absolute positioning of components (null layouts and setBounds ) as this will lead to frustration, grief and poor GUI's. 避免绝对定位组件(空布局和setBounds ),因为这会导致沮丧,悲伤和不良的GUI。 Use layout managers 使用布局管理器
  • Consider learning JavaFX instead of Swing since JavaFX is being supported actively by Oracle while Swing is not. 考虑学习JavaFX而不是Swing,因为Oracle积极支持JavaFX,而Swing不支持。

You just have to update your JLabel ! 您只需要更新您的JLabel即可!

JLabel lblCash = new JLabel("Cash: " + stats.cash);

lblCash.setForeground(Color.WHITE);
lblCash.setBounds(10, 649, 162, 14);
lblCash.setFont(new Font("AirbusMCDUa", Font.BOLD, 15));
contentPane.add(lblCash);

JButton btnAddCash = new JButton("ADD 10,000");
btnAddCash.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        stats.cash += 5000;                        // += is faster
        lblCash.setText("Cash: " + stats.cash);    // <-- Here
    }
});
contentPane.add(btnAddCash);                       // <-- Here

JButton debugBtn = new JButton("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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