我想做一个30秒倒计时进度条 
我直接继承了JProgressBar,然后再进度条的构建式中加入了一个SwingWorker,我想让SwingWorker每秒钟把计时减一,然后更新显示到JProgressBar上(准备用PropertyChangeListener),请问这个“每秒计时减一”怎么做呢?
因为同时会有别的操作,计时任务准备用doInBackground(),但是我不知道怎么做“每秒”这个间隔。能用Timer么? 或者干脆用wait()? 谢谢指点了

解决方案 »

  1.   

    目前我是這麼做的
    public class Progress extends JProgressBar {
    int second;
    countDown task;
    public Progress() {
    super(0, 30);
    second = 30;
    setValue(30);
    task = new countDown();
    task.execute(); } class countDown extends SwingWorker<Object, Object> { @Override
    protected Object doInBackground() throws Exception {
    for (; second > 0; second--) {
    Progress.this.setValue(second);
    wait(1000);
    }
    return null;
    }
    //cancel();
    @Override
    protected void done() {
    finish();
    }
    } // this happens both when the player respond, or times out
    public void finish() {
    task.cancel(true);
    }
    }