简体   繁体   English

更改JLabel的文本 - 初学者

[英]Change the text of a JLabel - Beginner

My code; 我的代码;

package com.test;

import java.awt.EventQueue;

public class TestGU {

    private JFrame frame;
    private JLabel la;

    /**
     * Launch the application.
     */
    public  void mainM() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestGU window = new TestGU();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void redefine(String text){
         la.setText(text);
    frame.repaint(); 

    }

    /**
     * Create the application.
     */
    public TestGU() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        la = new JLabel("New label");
        frame.getContentPane().add(null);
    }

}

I am trying to change the Text of the Label from the main method (which is a separate class) shown below; 我试图从主方法(这是一个单独的类)更改标签的文本,如下所示;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        TestGU g = new TestGU();
        g.mainM();
        g.redefine("New Value");
}
}

1.) When the main method is executed, i expected the label to have the text "New Value", but it still contains the text New label . 1.)当执行main方法时,我希望标签具有文本“New Value”,但它仍然包含文本New label Nothing has changed, how can i correct this? 什么都没有改变,我怎么能纠正这个?

It looks like you are creating two instances of TestGU and your redefine method changes the value of a label on an instance that isn't displayed. 看起来您正在创建两个TestGU实例,并且您的重redefine方法会更改未显示的实例上的标签值。

Just checking my theory now.... 现在就检查一下我的理论......

Edit: 编辑:
You don't need to create the 2nd instance; 您不需要创建第二个实例; your mainM method should be; 你的mainM方法应该是;

public void mainM() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

PS - I assume that your initialise method actually has this line right? PS - 我认为你的初始化方法实际上有这条线吗?

frame.getContentPane().add(la);

rather than add(null) as that doesn't work at all. 而不是add(null) ,因为它根本不起作用。

Have a look over this example for ideas. 看看这个例子的想法。

TestGU

import java.awt.EventQueue;
import javax.swing.*;

public class AnotherClass {

   public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
               TestGU g = new TestGU();
               g.redefine("New Value");
            }
        };
        EventQueue.invokeLater(r);
    }
}

class TestGU {

    private JFrame frame;
    private JLabel la;

    public void redefine(String text){
         la.setText(text);
    }

    /**
     * Create the application.
     */
    public TestGU() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        la = new JLabel("New label");
        frame.getContentPane().add(la);
        frame.pack();
        frame.setVisible(true);
   }
}

Never use JFrame as top level container. 切勿将JFrame用作顶级容器。

Components added to JFrame are not registeder for listening by AWTEvent queue. 添加到JFrame的组件不是AWTEvent队列侦听的注册器。

So your setText() does not create proper event to component to be repainted. 因此,您的setText()不会为要重新绘制的组件创建正确的事件。

setContentPane( new JPanel(){{ add(la); }} );

And it will work as expected, without calling any paint/repaint methods. 并且它将按预期工作,而不调用任何绘制/重绘方法。

You created 2 instances of TestGU (1 in Test and 1 in mainM() in TestGU ) and only 1 was visible. 你创造的2个实例TestGU (1 Test中和1 mainM()TestGU )只有1例可见。 Change your mainM() method to this: mainM()方法更改为:

/**
 * Launch the application.
 */
public void mainM() {
    this.frame.setVisible(true);
}

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

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