使用setText方法时JLabel不更新 [英] JLabel does not update when using setText method

查看:87
本文介绍了使用setText方法时JLabel不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前正在从事的项目中,我想通过Jlabel显示一些信息. GUI的其他位置有一些按钮和文本字段,这些按钮和文本字段允许更改所说的信息,我想更新JLabel,但文本永不更改,或在启动时更新.

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.

我曾尝试使用并发来更新标签,如本网站上其他问题所建议的那样,但是我对标签的更新并不满意.并发确实可以根据需要更新文本字段和comboBoxes.

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,

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

JPanel名为generalPanel

The JPanel named generalPanel

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

标签的文本以Empty字符串开头,并且在窗口启动时,可以通过从相关来源获取信息来更新标签.我能否对JLabel为何从不更新和显示其信息有一些见解?

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?

推荐答案

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

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

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.

从其中带有文本的标签开始,然后将文本设置为空字符串即可正常工作.

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

这篇关于使用setText方法时JLabel不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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