如何从另一个线程更新 JProgressBar.setValue(int)?我的次要目标是尽可能少地上课。
这是我现在拥有的代码:
// Part of the main class....
pp.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
new Thread(new Task(sd.getValue())).start();
}
});
public class Task implements Runnable {
int val;
public Task(int value){
this.val = value;
}
@Override
public void run() {
for (int i = 0; i <= value; i++){ // Progressively increment variable i
pbar.setValue(i); // Set value
pbar.repaint(); // Refresh graphics
try{Thread.sleep(50);} // Sleep 50 milliseconds
catch (InterruptedException err){}
}
}
}
pp 是一个 JButton 并在单击 JButton 时启动新线程。
pbar 是 Main 类中的 JProgressBar 对象。
我怎样才能更新它的价值?(进展)
上面 run() 中的代码看不到 pbar。