栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Swing计时器延迟进度条的加载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Swing计时器延迟进度条的加载

(从长远来看)使用可能更容易

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497479.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号