从另一个类更改 JLabel 文本 [英] Change JLabel text from another class

查看:49
本文介绍了从另一个类更改 JLabel 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下绘制标签的类.(我这里只给出了部分代码).一切正常,标签显示.

I have the following class that draws a Label. (I have only given part of the code here). Everyhting works fine, the label gets displayed.

现在,我有另一个名为 Caller 的类.我有一种方法可以用来更改此标签的值.我该怎么做

Now, i have another class called Caller Class. I have a method in that where i will use to change the value of this label. how can i do that

public class MyClass{

    private JLabel label;

    MyClass(){

       run();
    }

   public void editTheLabelsValue (String text) {
      label.setText(text);
      frame.repaint(); 
    }


    run(){
            .... // there were more code here, i removed it as it's not relevant to the problem
        label = new JLabel("Whooo");
        label.setBounds(0, 0, 50, 100);
        frame.getContentPane().add(label);
            .....
    }

稍后,我将使用以下类来更改上述标签的文本.我怎样才能做到这一点.

later on, i will be using the following class to change the text of the above label. How can i do this.

public class Caller {

void methodA(){
MyClass mc = new MyClass();
mc.editTheLabelsValue("Hello");
}

}

1.) 执行 methodA() 时,文本 Hello 未显示在 Label 字段上.它仍然是 Whooo.我该如何纠正这一点.一旦该方法被执行,我希望标签文本为 Hello .

1.) When the methodA() is executed, the text Hello is not getting displayed on the Label field. it still remains as Whooo. How can i correct this. I want the label text to be Hello once that method has been executed.

推荐答案

我能看到的直接问题是,您要么使用 null 布局,要么不了解布局管理器如何工作.

The immeditate problem I can see is to appears that you are either using a null layout or your don't understand how layout managers work.

以下代码通过setText 方法调用更新子类中主类的标签.该方法每秒调用一次

The following code updates the label from the main class in a sub class via a setText method call. This method is called every second

public class PaintMyLabel {

    private int counter = 0;

    public static void main(String[] args) {
        new PaintMyLabel();
    }

    public PaintMyLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final MasterPane master = new MasterPane();

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(master);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(1000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        counter++;
                        master.setText("Now updated " + counter + " times");
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();

            }
        });
    }

    public class MasterPane extends JPanel {

        private JLabel label;

        public MasterPane() {
            label = new JLabel("Original text");
            setLayout(new GridBagLayout());
            add(label);
        }

        public void setText(String text) {
            label.setText(text);
        }

    }

}

如果您正在使用 null 布局,请停止它.只是不要.使用 null 布局的次数很少,我怀疑这不是其中之一.

If you're using a null layout, then stop it. Just don't. There are only a very small number of times you would ever use a null layout and I suspect this isn't one of them.

这篇关于从另一个类更改 JLabel 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆