timer = new javax.swing.Timer(8, createTextLoadAction());
      timer.start();
public Action createTextLoadAction() {
         return new AbstractAction("text load action") {
             public void actionPerformed (ActionEvent e) {
                 if(jProgressBar1.getValue() < jProgressBar1.getMaximum()) {
                    jProgressBar1.setValue(jProgressBar1.getValue() +1);
                    tmp_CheckDb.getResultCheck(jProgressBar1.getValue());//你的程序
                    jLabel2.setText("目前已经检查:"+jProgressBar1.getValue()+"/"+jProgressBar1.getMaximum());
                 } else {
                     if(timer != null) {
                         timer.stop();
                         timer = null;
                     }
                 }
             }
         };
     }

解决方案 »

  1.   

    谢谢。从你的代码中,我怎么确定jProgressBar1.getMaximum()的值。因为我要执行一段耗时操作。但我没法确定要多少时间(30分钟只是一个例子)。所以我觉得没法确定getMaximum()的值。另外一点就是如果任务不能很快的完成,也许应该用SwingWorker 而不是Timer.
      

  2.   

    timer = new javax.swing.Timer(8, createTextLoadAction());//关键是这句了。
          timer.start();
      

  3.   

    我从别处找来了这样一个例子,不知道对你又无帮助package cosmos.alm;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;/** * 事件处理过程中UI的刷新 * @author Bruce * @version 1.0 */
    public class TestUIUpdate2 {
      public TestUIUpdate2() {
        TestUIUpdate2Frame frame = new TestUIUpdate2Frame();
        frame.pack();
        frame.setVisible(true);
      }  public static void main(String[] args) {
        new TestUIUpdate2();
      }
    }class TestUIUpdate2Frame
        extends JFrame {
      JTextPane pane = new JTextPane();
      JButton button = new JButton("action...");
      TestUIUpdate2Frame() {
        init();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            new Thread() {
                    public void run() {
                            try {
                                   showMessage("step one...");
                                    Thread.sleep(3000);
                                    showMessage("\nstep two...");
                                    Thread.sleep(3000);
                                    showMessage("\nfinished.");
                                    Thread.sleep(3000);
                            }
                            catch (InterruptedException ie) {
                                    //ignored
                            }
                    }
            }.start();      }
        });
      }
      private void showMessage (final String msg) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            pane.setText(pane.getText() + msg);
          }
        });
      }  private void init() {
        pane.setPreferredSize(new Dimension(300, 200));
        Container content = getContentPane();
        content.setLayout(new BorderLayout());
        content.add(pane, BorderLayout.CENTER);
        content.add(button, BorderLayout.SOUTH);
      }
    }
    它把按钮的action事件坐到一个线程里,然后用SwingUtilities.invokeLater调用
      

  4.   

    http://www.javaresearch.org/article/showarticle.jsp?column=287&thread=4429
      

  5.   

    谢谢大家的帮助,我已经解决了这个问题。事实上只要把耗时操作放到一个新线程里去就够了。然后在事件中运行这个线程。
    Thread t = new Thread(){
            public void run(){
             //我的耗时操作
            }
        };
        t.start() ;很感谢ManFirst 和xxisxx, 但我觉得binny给我的链接很好,所以我把分说给他。祝大家圣诞快乐。