Swing JProgressBar 没有像我期望的那样重新绘制
大家好,我有一个非常简单的问题,应该有人能够帮助我解决。我想要的只是一个带有更新进度条的小框架,现在它没有更新:
final JProgressBar bar = new JProgressBar(0,250000);
bar.setValue(1000);
bar.setIndeterminate(false);
JOptionPane j = new JOptionPane(bar);
final JDialog d = j.createDialog(j,"Expierment X");
d.pack();
d.setVisible(true);
bar.setValue(40000);
40,000 值没有显示,只有可怜的 1000。我宁愿不必编写任何类来处理重绘调用或无论涉及什么(永远没有使用 Swing)。
谢谢!
Hey all, I have a pretty simple problem someone should be able to help me with. All I want is a small frame with a progress bar that updates, right now it's not updating:
final JProgressBar bar = new JProgressBar(0,250000);
bar.setValue(1000);
bar.setIndeterminate(false);
JOptionPane j = new JOptionPane(bar);
final JDialog d = j.createDialog(j,"Expierment X");
d.pack();
d.setVisible(true);
bar.setValue(40000);
The 40,000 value doesn't show up, only the measly 1000. I'd prefer to not have to write any classes to handle repaint calls or whatever is involved in doing that (haven't used Swing in forever).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
createDialog
会阻塞,因此直到您在对话框上单击“确定”后才会调用bar.setValue
。您应该在不同的线程中更新进度条。
例如:
This is because
createDialog
blocks sobar.setValue
will not be called until you hit OK on the dialog.You should update the progress bar in a different thread.
For example:
您需要确保从事件调度线程调用
setValue
方法。您可以使用SwingUtilities.invokeLater
来实现此目的。You need to make sure that the
setValue
method gets called from the Event Dispatch Thread. You can useSwingUtilities.invokeLater
for that.