简体   繁体   English

以编程方式设置JLabel文本

[英]Setting JLabel text programmatically

So I'm trying to make the JLabel work in this code. 因此,我正在尝试使JLabel在此代码中工作。 I can get the button and Action Listener to work but not the Label. 我可以使按钮和动作侦听器正常工作,但不能让Label工作。 MyDice is where I create the Panels and buttons. 我在MyDice中创建面板和按钮。

public class MyDice
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("MyDice v1.0");
        frame.setSize(800,600);
        frame.setLocation(560, 240);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(200,200,200));
        panel.setSize(800,600);
        frame.add(panel);
        panel.setVisible(true);

        JButton button_d4    = new JButton("Roll d4");
        panel.add(button_d4);
        button_d4.addActionListener   (new MyRoll(4,panel));
    }
}

And the MyRoll is where I got the Action Listener that does something when I click the buttons. MyRoll是我单击按钮时执行某些操作的动作侦听器的地方。

public class MyRoll implements ActionListener
{
    int dice;
    JPanel panel;

    public MyRoll (int dice, JPanel panel)
    {
        this.dice = dice;
        this.panel = panel;
    }

    public void actionPerformed(ActionEvent e)
    {
        rollDice(dice,1);
    }

    public void rollDice (int dice, int times)
    {
        int r=0;
        for (int i=0; i<times;i++)
        {
            double rand = Math.random();
            rand = rand*dice + 1;
            r = (int) rand;
        }

            System.out.println("You rolled "+r+" out of "+dice);

        JLabel output = new JLabel();
        output.setText("You rolled "+r+" out of "+dice);
        panel.add(output);

    }
}

However, this last part does not work. 但是,这最后一部分不起作用。 Any ideas why? 有什么想法吗?

Use a JLabel and set the Label Message in your logic 使用JLabel并在逻辑中设置标签消息

REVISION for the updated CODE POSTED 修订版已发布的代码

You should set up the Label outside of the Logic, added to the Panel with initial empty value and then on your logic you change the Label's Value 您应该在逻辑之外设置标签,使用初始空值添加到面板中,然后在逻辑上更改标签的值

ie

//Setting up your GUI
JLabel label = new JLabel("");
panel.add(label);


//Within your Logic method
System.out.println("Printing info");
label.setLabel("Printing info");

That way you are not constantly adding a JLabel to your Panel 这样,您就不会在面板中不断添加JLabel

Look at the following code: 看下面的代码:

    JLabel output = new JLabel(); 
    output.setText("You rolled "+r+" out of "+dice); 
    panel.add(output);

Here you are adding a new JLabel to the panel (which happens to be in your main frame) every time you click the button. 每次您单击按钮时,都会在面板中添加一个新的JLabel (恰好在主框架中)。 However, you there is nothing to tell it to repaint itself. 但是,您没有什么可以告诉它重新绘制自身的。

My suggestion to fix this is to create the JLabel and add it to the JPanel in main() and pass the JLabel reference to your MyRoll class. 解决此问题的建议是创建JLabel ,并将其添加到main()JPanel中,然后将JLabel引用传递给MyRoll类。 Then you can simply call setText() any time you wish. 然后,您可以随时随意调用setText() You really don't need a reference to the JPanel in MyRoll ; 您确实不需要MyRollJPanelMyRoll you just need a reference to the JLabel . 您只需要引用JLabel

ps I want to make a few comments on your main() method. ps我想对您的main()方法发表一些意见。 In particular, if you organize your code differently, you can simplify it a little bit. 特别是,如果您以不同的方式组织代码,则可以稍微简化一下代码。 Typically creating a Swing GUI follows these steps: 通常,创建Swing GUI的步骤如下:

  1. Create a JFrame . 创建一个JFrame

  2. Create a JPanel . 创建一个JPanel

  3. Add components to the JPanel . 将组件添加到JPanel

  4. Add the JPanel to the JFrame . JPanel添加到JFrame

  5. Set the JFrame visible. JFrame设置为可见。

Notice that you only need to call setVisible() on the JFrame , if you do it very last. 注意,如果最后执行一次,只需要在JFrame上调用setVisible() This will automatically call setVisible() on the JPanel and all its subcomponents. 这将自动调用JPanel及其所有子组件上的setVisible()

If you are looking for a refresh method, there is none that I know of for . 如果您正在寻找一种刷新方法,那我就不知道了。 Instead you can set up a thread, that updates the output depending on the logic you provide. 相反,您可以设置一个线程,该线程根据您提供的逻辑来更新输出。

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

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