[英]Implementing method into the progress Bar
Here I got an action listener of a Button and a method to make iterate the progress Bar, how can I implement the method into the progress Bar? 在这里,我获得了一个Button的动作侦听器以及使进度条迭代的方法,如何将方法实现到进度条中?
I had tried pb.setString(iterator); 我试过了pb.setString(iterator); but obviously, it didn't work because SetString only accepts Strings.
但显然,它不起作用,因为SetString仅接受字符串。
public void actionPerformed(ActionEvent arg0)
{
pb = new JProgressBar(0,100);
pb.setBounds(80, 80, 160, 30);
pb.setValue(0);
pb.add(iterator());
pb.setStringPainted(true);
pb.setVisible(true);
frame.add(pb);
}
});
void iterator() throws InterruptedException
{
int i = 0;
while(i < 100)
{
pb.setValue(i);
i = i+5;
Thread.sleep(300);
}
}
I would create a SwingWorker . 我将创建一个SwingWorker 。
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
protected Void doInBackground() {
//do stuff
setProgress(progressValue); //when you want to update the progress, do this
}
}
worker.addPropertyChangeListener(new PropertyChangeListener() { //this runs whenever the progress is changed, to actually set the progress of the JProgressBar
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
pb.setValue(progress);
}
}
});
worker.execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.