问题描述
问题已解决!!!!!!
非常感谢trashgod和HoverCraftFullOfEels!我终于通过使用下面的例子并略微改变它来获得这个概念。更改允许缩放进度条(默认为100个单位)。
再次感谢您的耐心和愿意解决这个问题。意味着很多家伙,
~Kyte
QUESTION SOLVED!!!!!!Many thanks to trashgod and HoverCraftFullOfEels! I finally got the concept by using the below example and altering it slightly. The alteration allows scaling the progress bar (by default is 100 units).Again, THANK YOU for your patience and willingness to work through this. Means a lot guys,~Kyte
ps - + 1全部')
ps - +1's all 'round :)
/** @see http://stackoverflow.com/questions/4637215 */
public class Threading_01 extends JFrame {
private static final String s = "0.00";
private JProgressBar progressBar = new JProgressBar(0, 100);
private JLabel label = new JLabel(s, JLabel.CENTER);
public Threading_01() {
this.setLayout(new GridLayout(0, 1));
this.setTitle("√2");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(progressBar);
this.add(label);
this.setSize(161, 100);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void runCalc() {
// progressBar.setIndeterminate(true);
TwoWorker task = new TwoWorker();
task.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
progressBar.setIndeterminate(false);
progressBar.setValue((Integer) e.getNewValue());
System.out.println("**: " + e.getNewValue());
}
}
});
task.execute();
}
private class TwoWorker extends SwingWorker<Double, Double> {
private static final int N = 734;
private final DecimalFormat df = new DecimalFormat(s);
@Override
protected Double doInBackground() throws Exception {
int cntr = 1; //
double d1;
double d2;
double d3;
for (int i = 0; i <=N; i++) {
d1 = N;
d2 = i;
d3 = d2/d1;
d3 = d3 * 100;
System.out.println(i + "++ " + d3);
if(d3 >= cntr){
System.out.println(i + "UPDATE");
cntr++;
setProgress(cntr);
publish(Double.valueOf(cntr));
Thread.sleep(250); // simulate latency
}
}
return Double.valueOf(0);
}
@Override
protected void process(List<Double> chunks) {
for (double d : chunks) {
label.setText(df.format(d));
System.out.println(": " + d);
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Threading_01 t = new Threading_01();
t.runCalc();
}
});
}
}
推荐答案
更新进度条通常在 PropertyChangeListener
中处理,如图所示。另请参阅此相关的。如果您的工作人员以后可以提供更明确的进展,则调用 setIndeterminate(true)
是一个合理的初始设置。
Updating a progress bar is usually handled in a PropertyChangeListener
, as shown here. See also this related example. Invoking setIndeterminate(true)
is a reasonable initial setting if your worker can later offer more definitive progress.
这篇关于在下载过程中使用swingworker更新JProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!