【发布时间】:2017-02-27 11:00:28
【问题描述】:
这是我启动应用程序的主要方法。 JFrame 加载成功。当我添加 WHILE-Loop 部分 做一些后台工作时,我使用一些数据显示在我的 JFrame 上,我的 JFrame 没有正确加载(见下图)。
public static void main(String[] args) throws IOException {
if (Config.checkIfConfigExists() == true) {
/*
* Starten der Anwendung
*/
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frmServicenowHelper.invalidate();
window.frmServicenowHelper.validate();
window.frmServicenowHelper.repaint();
window.frmServicenowHelper.setVisible(true);
while (true) {
// the part that makes it error
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
} else {
Notifications.alertMSGConfig("Config not found. Create one?");
}
}
如您所见,JFrame 冻结并显示其背景。
我发现它与 Threads 和正确处理有关(我认为我在错误的地方使用了某些东西)但我自己无法修复它。
背景知识:
我想每 5 分钟从一个 URL 获取一个 JSON 字符串(该方法正在工作 - 我想在框架上调用并显示结果)(因此是 while 循环)。
编辑:
我尝试了这个可以正确加载框架但使循环(我需要)无用:
while (true) {
Main window = new Main();
window.frmServicenowHelper.invalidate();
window.frmServicenowHelper.validate();
window.frmServicenowHelper.repaint();
window.frmServicenowHelper.setVisible(true);
break;
}
【问题讨论】:
-
你在重新粉刷之前睡了一段时间吗?您确定 JFrame 上没有运行多个线程吗?
-
@AlexBaranowski 我在任何时候都没有睡觉 - 我很确定它只有一个线程。添加 break; 使其工作,但使循环无用,这是我需要的
标签: java multithreading jframe