【问题标题】:Change the text of a JLabel - Beginner更改 JLabel 的文本 - 初学者
【发布时间】:2012-11-16 11:21:55
【问题描述】:

我的代码;

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

}

我正在尝试从如下所示的主方法(它是一个单独的类)中更改标签的文本;

public class Test {

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

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

1.) 执行 main 方法时,我希望标签具有文本“新值”,但它仍然包含文本 New label。什么都没有改变,我该如何纠正?

【问题讨论】:

  • frame.getContentPane().add(null); 无法编译。那是你的真实代码吗?此外,您的代码运行 initialize 两次,因此有 2 个 TestGU 对象。
  • JLabel 也永远不会添加到框架中。
  • null 表示绝对布局对吗?
  • "null 表示绝对布局对吗?" 错误。这意味着在内容窗格中不添加任何内容。顺便说一句 - 使用布局!
  • 如何添加绝对布局?

标签: java swing jlabel


【解决方案1】:

看起来您正在创建两个 TestGU 实例,而您的 redefine 方法更改了未显示实例上的标签值。

现在只是检查我的理论....

编辑:
您不需要创建第二个实例;你的 mainM 方法应该是;

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

PS - 我假设你的初始化方法实际上有这一行,对吗?

frame.getContentPane().add(la);

而不是add(null),因为那根本不起作用。

【讨论】:

    【解决方案2】:

    查看此示例以获取想法。

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

    【讨论】:

    • 是的,但我想从另一个类更改标签的文本。
    • @AndrewThompson - 不要对他们太苛刻,至少他们没有继承JFrame
    • @Qwerky 实际上是“她”
    【解决方案3】:

    永远不要将 JFrame 用作顶级容器。

    添加到 JFrame 的组件未注册以供 AWTEvent 队列侦听。

    因此您的 setText() 不会为要重新绘制的组件创建适当的事件。

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

    它会按预期工作,无需调用任何绘制/重绘方法。

    【讨论】:

      【解决方案4】:

      您创建了 TestGU 的 2 个实例(Test 中的 1 个和 TestGU 中的 mainM() 中的 1 个)并且只有 1 个可见。将您的 mainM() 方法更改为:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-07
        • 2010-10-28
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多