Java GUI JProgressBar没有绘画 [英] Java GUI JProgressBar not painting
问题描述
我有一个GUI问题,我想整理出来,但我对于发生了什么感到困惑,希望你们中的一个人能解释一下。代码库太大而无法上传,但我会详细解释发生了什么:
I have a GUI problem that I would like to get sorted out, but I am baffled as to what's happening and hope one of you can explain it. The code base is way too large to upload however I will explain in detail what's happening:
我有一个类 ProgessBar
这是一个 JDialog
,包含一个摆动 JProgressBar
。我有一些吸气剂和制定者可以根据自己的喜好改变标准。但问题就出现了。
I have a class ProgessBar
which is a JDialog
containing a swing JProgressBar
. I have some getters and setters to change the bar to my liking however here comes the issue.
ProgressBar是在方法 myButtonActionPerformed <中生成的/ code>
The ProgressBar is spawned inside of a method myButtonActionPerformed
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
myButtonActionPerformed(evt);
}
});
基本上当用户点击此按钮时,处理开始并且 ProgressBar <产生/ code>。
essentially when the user hits this button, the processing begins and the ProgressBar
is spawned.
用户当前在他们面前有一个 JFrame
并且这个进展bar出现在 JDialog
中。在Netbeans中逐步调试模式后,我可以看到 JProgressBar
的值发生了变化,但是当我的程序处理时,条形图在视觉上保持0%,然后立即跳转到100它离开上面的添加动作侦听器方法的那一刻,几乎就像它等待重绘一样直到该侦听器之外。我不明白的是什么?有什么我可以调用它会强制它在这个 myButtonActionPerformed
方法内更新,而不是等到它没用。
The user currently has a JFrame
in front of them and this progress bar is popped up in a JDialog
. After stepping through debug mode in Netbeans, I can see the JProgressBar
's values getting changed, but the bar visually stays at 0% while my program is processing then instantly jumps to 100% the moment it leaves the add action listener method above, almost as if it was waiting to repaint until out of that listener. What am I not understanding? Is there something I can call that will force it to update inside of this myButtonActionPerformed
method instead of waiting until it's useless.
推荐答案
你在做EDT的那段时间吗?请记住,AWT / Swing有一个专门的线程来完成GUI工作 - 处理事件处理程序,重新绘制GUI等。如果你在该线程上进行长时间运行,Swing将不会重新绘制。
Are you doing whatever takes that time in the EDT? Keep in mind that AWT/Swing have a dedicated thread that does GUI work – handling event handlers, repainting the GUI, etc. If you do long-running things on that thread, Swing will not repaint.
尝试在另一个线程中执行您的任务并相应地更新进度条。但是,使用 SwingUtilities.invokeLater
或 invokeAndWait
来更新进度条,以确保在EDT上进行GUI更新。事情变得非常奇怪。
Try performing your task in another thread and update the progressbar from there accordingly. Use SwingUtilities.invokeLater
or invokeAndWait
to update the progressbar, though, to ensure that the GUI updates happen on the EDT. Otherwise things get very weird.
这篇关于Java GUI JProgressBar没有绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!