Change the text of a JLabel - Beginner

纵然是瞬间 提交于 2019-12-23 19:02:57

问题


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. Nothing has changed, how can i correct this?


回答1:


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.

Just checking my theory now....

Edit:
You don't need to create the 2nd instance; your mainM method should be;

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?

frame.getContentPane().add(la);

rather than add(null) as that doesn't work at all.




回答2:


Have a look over this example for ideas.

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



回答3:


Never use JFrame as top level container.

Components added to JFrame are not registeder for listening by AWTEvent queue.

So your setText() does not create proper event to component to be repainted.

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

And it will work as expected, without calling any paint/repaint methods.




回答4:


You created 2 instances of TestGU (1 in Test and 1 in mainM() in TestGU) and only 1 was visible. Change your mainM() method to this:

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


来源:https://stackoverflow.com/questions/13415584/change-the-text-of-a-jlabel-beginner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!