如何使用Swing计时器延迟一个进度条的加载(How to use the Swing Timer

2019-07-28 17:57发布

我需要找到一种方法,使用Swing定时器与一个进度条。 我尝试使用Thread.sleep()方法,但它坠毁应用程序时,我用它。 使用Swing的计时器的任何方式,而不是睡眠()?

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是它加载速度。 状育苗是类中的方法是,PC代表%,有多少是做了栏上将会持续显示。 o.Eros打开anotherr GUI。

Answer 1:

它可能更容易(从长远来看)使用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还提供了一个进度监控的功能,所以,你的情况,这会你就不必使用publish / process的API的一部分,如果你不想。 这将允许您将附加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();
    }

}


Answer 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();
}


文章来源: How to use the Swing Timer to delay the loading of a progress bar