java 使用 setText 方法时 JLabel 不更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17578390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:22:29  来源:igfitidea点击:

JLabel does not update when using setText method

javaswingjlabel

提问by JME

In the project I am currently working on I have several pieces of information that I would like to make visible via Jlabel. There are a few buttons and textfields elsewhere in the GUI that allow for altering said information I would like to update the JLabel but the text never changes, or updates upon startup.

在我目前正在处理的项目中,我希望通过 Jlabel 显示几条信息。GUI 中的其他地方有一些按钮和文本字段,允许更改所述信息我想更新 JLabel 但文本永远不会更改,或在启动时更新。

I have attempted to use concurrency to update the labels, as suggested in other questions on this site, but I have had no luck with the labels updating. The concurrency does work with updating textfields and comboBoxes as needed.

我曾尝试使用并发来更新标签,如本网站上其他问题中所建议的那样,但我在标签更新方面没有运气。并发确实可以根据需要更新文本字段和组合框。

The current iteration of my code looks as follows,

我的代码的当前迭代如下所示,

The JFrame

JFrame

//// This is a snippet from the JFrame

public void start()
{
    this.setSize(900, 700);
    this.setVisible(true);

    devicePanel.populateDeviceDefinitions();
    updateServiceInfo();
    updateCommandInfo();
    startUpdateTimer();
}

public void updateServiceInfo()
{
    EventService service = JetstreamApp.getService();

    generalPanel.updateServiceInfo(service.getBaseUri(), 
            service.getAccessKey(), String.valueOf(service.getWindowTime()));
}

public void updateCommandInfo()
{
    JetstreamServiceClient client = JetstreamApp.getClient();

    generalPanel.updateCommandInfo(client.getBaseUri(), client.getAccessKey());
}

The JPanel named generalPanel

名为generalPanel的JPanel

//// This is a snippet from the generalPanel
//// All of the variables in the following code are JLabels

public void updateServiceInfo(String baseUrl, String accessKey,
        String windowTime)
{
    serviceUrl.setText(baseUrl);
    serviceAccessKey.setText(accessKey);
    serviceWindowTime.setText(windowTime);
}

public void updateCommandInfo(String baseUrl, String accessKey)
{
    commandUrl.setText(baseUrl);
    commandAccessKey.setText(accessKey);
}

The labels start with an Empty string for their text and upon window start it is intended that they be updated by grabbing the information from the relevant sources. Can I please have some insight as to why the JLabels never update and display their information?

标签以其文本的空字符串开头,并且在窗口启动时旨在通过从相关来源获取信息来更新它们。我能否了解为什么 JLabels 从不更新和显示他们的信息?

回答by JME

How did you create the JLabel? If the text starts out as "", and you've created it with new JLabel(""), the width of the JLabel may be initialized to 0 and then none of your text would show up when you update it. I believe I've had that sort of problem in the past. As a test, try using new JLabel("aaaaaaaaaa") or some longer string to create the label, then setText(""); then later, when you setText(somethingElse), see if that causes text to show up. If it does, then the width is probably the problem and you can work on it from there. – ajb 19 mins ago

你是如何创建 JLabel 的?如果文本以 "" 开头,并且您已使用 new JLabel("") 创建它,则 JLabel 的宽度可能会被初始化为 0,然后当您更新它时,您的任何文本都不会显示。我相信我过去也遇到过这样的问题。作为测试,尝试使用 new JLabel("aaaaaaaaaa") 或一些更长的字符串来创建标签,然后 setText(""); 然后稍后,当您 setText(somethingElse) 时,看看是否会导致文本显示。如果是这样,那么宽度可能是问题所在,您可以从那里开始处理。– ajb 19 分钟前

This comment is the actual answer, when creating a JLabel with an empty string as the text the label's dimensions do not get set properly when using WindowBuilderPro. My labels did exist, and were being updated with the code provided in my question but the labels were not visible.

此评论是实际答案,当使用 WindowBuilderPro 创建带有空字符串的 JLabel 作为文本时,标签的尺寸未正确设置。我的标签确实存在,并且正在使用我的问题中提供的代码进行更新,但标签不可见。

Starting with a label that has text in it, then setting the text to an empty string works properly.

从包含文本的标签开始,然后将文本设置为空字符串可以正常工作。

回答by Md Ayub Ali Sarker

The method paintImmediately() can be used to cause a Swing component to get updated immediately. after setText(), you should call paintImmediately() like below.

方法paintImmediately() 可用于使Swing 组件立即更新。在 setText() 之后,你应该像下面一样调用paintImmediately()。

jLabel.setText("new text")
jLabel.paintImmediately(jLabel.getVisibleRect());

回答by David

You should try to call revalidate() or repaint() on the component that contains your JLabels.

您应该尝试在包含 JLabel 的组件上调用 revalidate() 或 repaint()。

Cheers

干杯