这样一段代码,希望能够在执行的时候,显示一个进度条。但是每次进度条都要等到执行完了才显示出来,为什么呢?
Progress p=new Progress(poi);
Thread th=new Thread(p);
try{
th.start();
//一段需要很长时间的代码
//th.interrupt();
}catch(Exception e){
//th.interrupt();
}
package com.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.Timer;
public class Progress implements Runnable {
Timer tm;
ProgressMonitor pm;
JFrame poi;
int i;
public Progress(JFrame poi){
this.poi=poi;
}
public void run() {
ActionListener actionListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
pm.setProgress(++i==100?1:i);
}
};
System.out.println(123);
tm=new Timer(200,actionListener);
pm=new ProgressMonitor(poi,"","正在导入,请稍候...",1,100);
pm.setMillisToDecideToPopup(0);
pm.setMillisToPopup(0);
tm.start();
}
}
本文探讨了一个Java Swing应用中进度条的实现问题。通过创建一个单独的线程来更新进度条,试图实现在长时间运行任务期间显示实时进度,但发现进度条只在任务完成后更新。文章提供了具体的代码示例。
69

被折叠的 条评论
为什么被折叠?



