我正在用非常简单的线程(延迟1秒)制作简单的游戏 线程有问题,我的代码有while(true)循环:
I'm making simple game with very simple thread (1 sec delay) got problem with the thread, I have while(true) loop with the code:
try { while (true) { Ltimer.setText(getTimeElapsed()); Thread.currentThread(); Thread.sleep(1000); // Thread sleeping for 1 second } } catch (Exception e) { JOptionPane.showMessageDialog(null, "error with timer"); }它只是每秒获取字符串并更新标签文本 当我尝试运行gui冻结时,只能看到黑色背景中的标签,所有按钮和bg img都消失了.试图解决 setVisible() repaint() 但一无所获..
it simply get string every second and updates label text when I'm trying to run it the gui freeze and I can only see the label in a black background, all buttons and bg img dissappeared. tried to fix with setVisible() repaint() but got nothing..
还有其他选择吗?
推荐答案在EDT期间不要使用Thread#sleep(int),那么 Swing的一致性,如果您需要延迟任何操作,请使用 java.swing.Timer ,例如此处缺少EDT
don' use Thread#sleep(int) during EDT, then you have issue with Concurency in Swing, if you need to delay any action use java.swing.Timer, example for EDT lack here
更多推荐
在JAVA中进行线程处理时未显示GUI
发布评论