如何使用Swing Timer延迟进度条的加载

时间:2013-03-27 20:25:32

标签: java swing timer progress-bar sleep

我需要找到一种方法来使用带有进度条的Swing Timer。我尝试使用Thread.sleep(),但是当我使用它时,它崩溃了应用程序。有什么方法可以使用Swing Timer而不是Sleep()?

public void piiEros(int dist)
{
    Pii pii = new Pii();
    pii.setVisible(true);
    for(int pc = 0;100 > pc; pc++)
    {
        try {
            Thread.sleep(dist/100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Trav.class.getName()).log(Level.SEVERE, null, ex);
        }
        pii.pg.setValue(pc);
    }
    pii.dispose();
    o.Eros();
}

注意:Pii是一个带有进度条的类。 Dist是它加载的速度。 Trav是该方法所在的类.Pc代表%,在条上显示了多少。 o.Eros打开另一个GUI。

2 个答案:

答案 0 :(得分:4)

使用SwingWorker可能更容易(从长远来看)。它提供了许多有用的方法来更新UI(从事件调度线程的上下文),同时允许继续在后台执行...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestSwingWorker02 {

    public static void main(String[] args) {
        new TestSwingWorker02();
    }

    public TestSwingWorker02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JProgressBar pb = new JProgressBar();
            add(pb);

            new ProgressWorker(pb, 40).execute();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class ProgressWorker extends SwingWorker<Void, Integer> {

        private int delay;
        private JProgressBar pb;

        public ProgressWorker(JProgressBar progressBar, int delay) {
            this.pb = progressBar;
            this.delay = delay;
        }

        @Override
        protected void process(List<Integer> chunks) {
            // Back in the EDT...
            pb.setValue(chunks.get(chunks.size() - 1)); // only care about the last one...
        }

        @Override
        protected Void doInBackground() throws Exception {
            for (int index = 0; index < 100; index++) {
                publish(index);
                Thread.sleep(delay);
            }
            return null;
        }

        @Override
        protected void done() {
            // Back in the EDT...
            //pii.dispose();
            //o.Eros();
        }

    }

}

SwingWorker允许您分离逻辑。在doInBackground方法中,您可以专注于需要在EDT之外操作的代码部分,您可以publish分别更新回EDT和process。当它全部done时,您可以根据需要进行清理。

SwingWorker还提供了进度监控功能,因此,在您的情况下,如果您没有使用,则不必使用API​​的publish / process部分我想。这将允许您将PropertyChangeListener附加到工作人员,而无需向其公开进度条。 (但我为这个例子做了)

public class ProgressWorker extends SwingWorker<Void, Integer> {

    private int delay;
    private JProgressBar pb;

    public ProgressWorker(JProgressBar progressBar, int delay) {
        this.pb = progressBar;
        this.delay = delay;
        // You can use a property change listener to monitor progress updates...
        addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
                    pb.setValue((Integer)evt.getNewValue());
                }
            }

        });
    }

    @Override
    protected Void doInBackground() throws Exception {
        for (int index = 0; index < 100; index++) {
            setProgress(index);
            Thread.sleep(delay);
        }
        return null;
    }

    @Override
    protected void done() {
        // Back in the EDT...
        //pii.dispose();
        //o.Eros();
    }

}

答案 1 :(得分:2)

您可以通过以下方式更改piiEros方法:

public void piiEros(int dist)
{
    final Pii pii = new Pii();
    pii.setVisible(true);
    javax.swing.Timer  timer = new javax.swing.Timer(dist/100, new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        pii.pg.setValue(pg.getValue()++);
        if ( pii.pg.getValue() > 100 )
        {
          timer.stop();
          pii.dispose();
          o.Eros();
        }
      }
    });
    timer.setInitialDelay(0);
    timer.setRepeats(true);
    timer.start();
}