如何更新j进度条
我的应用程序中有一个简单的 JForm 和一个 JDialog。 JDialog 框包含一个 JProgressBar eliment,我在 JDialog 中放置了一个方法,
public void updateProgress(int val){
prgProgress.setValue(val); //prgProgress-> name of the JProgressBar
}
用于更新进度栏。
当我尝试从 JForm 更新 JDialog 中的进度条时,JProgressBar 没有按预期更新,请告诉我可能是什么错误,
例如。
public class Status extends javax.swing.JDialog{
private javax.swing.JProgressBar prgProgress = new javax.swing.JProgressBar;
.....
public void updateProgress(int val){
prgProgress.setValue(val); //prgProgress-> name of the JProgressBar
}
.....
}
public class MyForm extends javax.swing.JInternalFrame {
Status s = new Status();
s.setVisible(true);
for(int i=0; i<100; i++){
s.updateProgress(i);
}
}
I have a simple JForm and a JDialog in my application. JDialog box contains a JProgressBar eliment and I'put a method in the JDialog as,
public void updateProgress(int val){
prgProgress.setValue(val); //prgProgress-> name of the JProgressBar
}
to update the progress bar.
When I try to update the progress bar in my JDialog from the JForm, JProgressBar doesn't update as expected please tell me what could be the error,
Ex.
public class Status extends javax.swing.JDialog{
private javax.swing.JProgressBar prgProgress = new javax.swing.JProgressBar;
.....
public void updateProgress(int val){
prgProgress.setValue(val); //prgProgress-> name of the JProgressBar
}
.....
}
public class MyForm extends javax.swing.JInternalFrame {
Status s = new Status();
s.setVisible(true);
for(int i=0; i<100; i++){
s.updateProgress(i);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对 Swing 中的并发性有疑问,Swing 是单一的线程化并且如果您从 EDT 更新 GUI,则不会发生任何事情,您必须实现 SwingWorker 用于更新 JProgressBar< /a>,带有说明的示例此处
You have a issue with Concurency in Swing, Swing is single threaded and nothing happends if you update GUI out of EDT, you have to implement SwingWorker for updating JProgressBar, example with descriptions here
首先,我从上面的代码中看不到您已将
JProgressBar
添加到JDialog
中,但我假设您已经这样做了。其次,您没有通过构造函数或成员函数
setMaximum()
设置JProgressBar
的最大值。这可能是没有进展的原因。参见:
Firstly, I can't see from your code above showing that you have added the
JProgressBar
into theJDialog
, but I assume you have done so.Secondly, you didn't set the maximum value of the
JProgressBar
, from either constructor or member functionsetMaximum()
. This might be a reason of showing no progress.See Also: