你好,我想用Swing Worker類的幫助來更新我的JProgress Bar。我在這個論壇上搜索了關於這個主題的其他問題,並拿了一些解決方案的代碼,但在我的情況下,我不工作:(我有兩個類:在第一個類中,我創建Swing工人類BitmapFilterParralelGrey的對象與此代碼:JProgress Bar not updating
Greyscale.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
int numThreads = Runtime.getRuntime().availableProcessors();
int HeightPerThread = image.getHeight()/numThreads;
BitmapFilterParallelGrey[] bitmapFilter = new BitmapFilterParallelGrey[numThreads];
int startRow = 0, endRow = 0;
for (int i = 0; i < numThreads; ++i) {
startRow = i * HeightPerThread;
endRow = startRow + HeightPerThread;
bitmapFilter[i] = new BitmapFilterParallelGrey(image,
startRow, endRow);
bitmapFilter[i].addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
BitmapViewer.ProgressBar.setIndeterminate(false);
BitmapViewer.ProgressBar.setValue((Integer) e.getNewValue());
}
}
});
bitmapFilter[i].execute();
}
for (int i = 0; i < numThreads; i++) {
try {
bitmapFilter[i].get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
showImage();
}
});
如SwingWorker類的執行應被擰i。在環路創建多個對象以我第二類BitmapFilterParallelGrey I濾波器在doInBackground(的圖像),並設置ProgressValue的代碼:。
public class BitmapFilterParallelGrey extends SwingWorker<Void, Void> {
BufferedImage image;
private int startRow, endRow;
public BitmapFilterParallelGrey(BufferedImage image, int startRow, int endRow){
this.image = image;
this.startRow= startRow;
this.endRow= endRow;
}
@Override
public Void doInBackground() throws Exception {
int width = image.getWidth();
for(int i=startRow; i<endRow; i++)
{
for(int j=0; j<width; j++)
{
//Filter image
}
setProgress(i/100);
Thread.sleep(10);
}
return null;
}
}
問題是進度條在過濾完成時更新(100%)。爲什麼不更新它吃了,而它在後臺過濾? 謝謝!