通过计时器在JDialog中设置动态JLabel文本 [英] Set dynamic JLabel text in a JDialog by timer
问题描述
我正在尝试制作一个JDialog,该对话框将向用户显示JLabel上的动态消息. 该消息应该是从1到10的计数(并且应该每秒更改一个数字). 问题是,当我进行调试时-它在"dia.setVisible(true);"之后立即停止,除非我将关闭JDialog,否则它将不会继续. 有什么可能的解决办法? 谢谢.
Im trying to make a JDialog that will show the user a dynamic message on a JLabel. The message should be a count from 1 to 10 (and it should be changing a number every second). the thing is , when im debugging it - it's stops right after the "dia.setVisible(true);" , and will not proceed unless i will close the JDialog . is there any possible way of fixing it? Thanks.
看看代码:
@Override
public void run() {
dia = new JDialog(parentDialog, true);
dia.setLocationRelativeTo(parentFrame);
String text = "Text ";
dia.setSize(300, 150);
jl = new JLabel(text);
dia.getContentPane().add(jl);
dia.setVisible(true);
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
jl.setText(text + " " + i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
推荐答案
-
不要在Swing GUI中使用
Thread.sleep(int)
,导致冻结直到Thread.sleep(int)
结束don't use
Thread.sleep(int)
for Swing GUI, caused freeze of untillThread.sleep(int)
ended使用
Swing Timer
代替通过使用Thread.sleep(int)
不使用
dia.setSize(300, 150)
,了解LayoutManager
的工作方式don't use
dia.setSize(300, 150)
, learn howLayoutManager
works这篇关于通过计时器在JDialog中设置动态JLabel文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!