在我目前正在进行的项目中,我希望通过 Jlabel 显示几条信息。 GUI 中的其他位置有一些按钮和文本字段,允许更改所述信息,我想更新 JLabel,但文本永远不会更改,或在启动时更新。
我尝试使用并发来更新标签,正如本网站上其他问题中所建议的那样,但我在标签更新方面没有运气。并发确实可以根据需要更新文本字段和组合框。
我的代码的当前迭代如下所示,
J框架
//// 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());
}
名为 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);
}
标签的文本以空字符串开头,并且在窗口启动时,旨在通过从相关源获取信息来更新它们。我能否了解一下为什么 JLabels 从不更新和显示其信息?
您是如何创建 JLabel 的?如果文本以“”开头,并且您使用 new JLabel("") 创建了它,则 JLabel 的宽度可能会初始化为 0,然后在更新它时不会显示任何文本。我相信我过去也遇到过这样的问题。作为测试,尝试使用 new JLabel("aaaaaaaaaa") 或一些更长的字符串来创建标签,然后 setText("");然后,当您 setText(somethingElse) 时,看看是否会导致文本显示。如果是这样,那么宽度可能是问题所在,您可以从那里开始解决。 – ajb 19 分钟前
此注释是实际答案,当使用空字符串作为文本创建 JLabel 时,使用 WindowBuilderPro 时标签的尺寸不会正确设置。我的标签确实存在,并且正在使用我的问题中提供的代码进行更新,但标签不可见。
从包含文本的标签开始,然后将文本设置为空字符串即可正常工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)