我在写入/更新文本区域时遇到问题。我从 readtemp 函数中获取一个值,并且在调用系统输出函数后可以看到结果,但文本区域中没有出现任何内容。可能是什么问题?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
URL temp;
try
{
temp = new URL("http://192.168.1.25/status.xml");
while (true)
{
System.out.println("Homerseklet: " + readtemp(temp));
jTextArea1.append(readtemp(temp));
}
}
catch (MalformedURLException ex)
{
Logger.getLogger(Download.class.getName()).log(Level.SEVERE, null, ex);
}
}
最佳答案
更正:这不会有帮助,因为无限循环仍然会永远阻塞 EDT...没关系!
你的 while 循环是一个非常糟糕的主意,但如果你坚持,你至少可以给 EDT 有机会通过异步分派(dispatch)你的附加来更新 UI:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jTextArea1.append(readtemp(temp));
}
});
关于java - 在 while 循环中更新 jTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30841166/