简体   繁体   English

JLabel未更新

[英]JLabel not updating

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! 因此,感谢您查看我的问题,无论如何,我一直试图在单击按钮和JPanel更新的地方制作cookie单击器的克隆(如果可以调用它的话),不幸的是JPanel不会更新,我只是找不到答案在其他任何地方,任何帮助都是宝贵的帮助,谢谢! 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();

}

Three things... 三件事...

One... 一...

Use of null layouts. 使用null布局。 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. 使用JLabelJButton ,除非您打算向其中实际添加一些东西,否则无需接触那里的布局管理器。

Make use of approriate layout managers 利用适当的布局管理器

Two... 二...

Calling setVisible on your frame before you've finished filling it out... 在完成填写之前在框架上调用setVisible ...

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... 您似乎期望, numCookies的值已更改,该标签就会神奇地...

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... ...因为null布局让我非常烦恼...

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

Set the text of the JLabel in the actionPerformed method like this: 像这样在actionPerformed方法中设置JLabel的文本:

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

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

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