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

在Java中执行过程而不会冻结JFrame

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

在Java中执行过程而不会冻结JFrame

是的,您可以使用一个

SwingWorker
想法,即一个任务需要花费大量时间在单独的线程(后台线程)中运行,因此您不会阻塞gui,并且Jframe不会冻结。这是一个完整的示例,我非常喜欢Swing
Worker示例

基本上以示例为例,您将创建自己的扩展

SwingWorker
覆盖的类
doInBackground

注意:您可以具有类似普通班级的字段。

范例:

class Worker extends SwingWorker<Void, String> {    private SomeClass businessDelegate;        private JLabel label;    @Override    protected Void doInBackground() throws Exception {       //here you make heavy task this is running in another thread not in EDT        businessDelegate.callSomeService();        setProgress(30); // this is if you want to use with a progressBar        businessDelegate.saveToSomeDatabase();        publish("Processes where saved");        return null;    }    @Override    protected void process(List<String> chunks){       //this is executed in EDT you can update a label for example       label.setText(chunks.toString());    }   //add setters for label and businessDelegate    }

您还了解了

process(..)
publish(..)
done()

并在您的客户端代码中放入。

SwingWorker<Void,String> myWorker = new Worker();myWorker.execute();


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

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

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