JProgressBar 不会在循环内更新 [英] JProgressBar does not update within the loop
问题描述
我目前正在自学 Java.我一直在尝试不同的东西,比如 JRadioButton
s、JcomboBox
es 等.现在,我正在尝试使用 JProgressBar
s 但它没有似乎工作正常.
I am currently self-studying Java. I've been trying different things like JRadioButton
s, JcomboBox
es etc. Now, I'm trying to use JProgressBar
s but it doesn't seem to work properly.
相关代码:
JProgressBar progress;
JButton button; //Fields of the class
progress=new JProgressBar(JProgressBar.HORIZONTAL,0,100);
button=new JButton("Done"); //Done from methods
progress.setValue(0);
progress.setStringPainted(true);
progress.setBorderPainted(true); //Also done from methods
button.addActionListener(this); //Also done from methods
当 button
被点击时,我想显示 JProgressBar 从 0% 到 100%.下面是 actionPerformed
方法的相关部分:
When button
is clicked, I want to show the JProgressBar go from 0% to 100%. Here is the relevant portion of the actionPerformed
method:
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<=progress.getMaximum();i++)
{
progress.setValue(i);
/*try{
Thread.sleep(10);
}catch(InterruptedException ex)
{
System.err.println("An error occured:"+ex);
ex.printStackTrace();
}*/
}
progress.setValue(progress.getMinimum());
}
我已将 progress
和 button
添加到 JPanel 中,并将面板添加到我使用 setVisible(true);
的 JFrame 中.
I've added both progress
and button
into a JPanel and the panel into a JFrame on which I use setVisible(true);
.
这里的问题是,每当我按下 JButton button
时,JProgressBar progress
不会从 0% 变为 100%.相反,什么也没有发生.如果我取消注释 try...catch
块,然后按下 button
,程序会冻结"一会儿然后继续.这一次,JProgressBar 也保持在 0%,我从未看到它移动.我还尝试了 repaint();
就在 try...catch
块之后,但发生了同样的事情.
The problem here is, whenever I press the JButton button
, the JProgressBar progress
does not go from 0% to 100%. Instead, nothing happens. If I uncomment the try...catch
block, and then press button
, the program "freezes" for a moment and then continues. This time too, The JProgressBar stays at 0% and I never see it moving. I've also tried a repaint();
just after the try...catch
block but the same thing happened.
我试过添加
System.out.println(i+"");
在 actionPerformed
中的 for
循环内,这会在终端中打印数字 0 到 100.所以,我确定循环运行了.
inside the for
loop in actionPerformed
and this printed numbers 0 to 100 in the terminal. So, I'm sure that the loop runs.
我该如何解决这个问题?
How can I solve this problem?
推荐答案
Swing 是单线程的,
Swing is single threaded,
所有更新都必须在 EDT 上完成,
all updates must be done on EDT,
当前 EDT 中的所有事件都在一瞬间绘制在 GUI 中,
all events in current EDT are painted in GUI in one moment,
Thread.sleep
在 EDT 中锁定事件的执行,重绘可以在 for_loop 结束时绘制,在所有来自Thread.sleep
执行,更多在 Oracle 跟踪中 - Swing 中的并发 和EventDispatchThreadThread.sleep
to lock execution of event in EDT, repaint can be painted at the end of for_loop, after all lock(s) fromThread.sleep
are executed, more in Oracle trail - Concurency in Swing and EventDispatchThreadAWT (Swing) Listener 的输出 -
ActionListener
,如果actionPerformed
中的所有事件都被执行了,那么应该只执行progress.setValue(progress.getMinimum());
,不管有没有Thread.sleep
output from AWT (Swing) Listener -
ActionListener
, should be done if all events insideactionPerformed
are executed, then there is executed onlyprogress.setValue(progress.getMinimum());
, doesn't matter if is thereThread.sleep
or notpublic void actionPerformed(ActionEvent e) -> progress.setValue(progress.getMinimum());
<小时>
使用
SwingWorker
或Runnable#Thread
和progress.setValue(i);
包裹到invokeLater
>
use
SwingWorker
orRunnable#Thread
withprogress.setValue(i);
wrapped intoinvokeLater
为了更好的帮助,尽快发布
SSCCE
/MCVE
,简短,可运行,可编译for better help sooner post an
SSCCE
/MCVE
, short, runnable, compilable这篇关于JProgressBar 不会在循环内更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!