SwingUtilities.invokeLater [英] SwingUtilities.invokeLater

查看:307
本文介绍了SwingUtilities.invokeLater的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是有关 SwingUtilities.invokeLater 。什么时候应该使用它?我必须每一次我需要更新GUI组件使用?这是什么究竟做什么?有没有给它的选择,因为它听起来并不直观,增加了不必要的貌似code?


解决方案

  

我必须每次我需要更新GUI组件使用?


不,如果你已经对事件调度线程(EDT)响应用户发起的事件,如点击和选择时,它总是这样的。 (在的actionPerformed 方法等,总是被称为美国东部时间)

如果你的的上不过EDT和想要做的GUI更新(如果要更新一些计时器线程图形用户界面,或者从一些网线等),你就会有到的进度的要由EDT进行更新。这就是这种方法对于

摇摆基本上是线程不安全的。即,需要在单个线程(EDT)执行与该API的所有交互。如果你需要从另一个线程执行GUI更新(计时器线程,线程联网,......),你需要使用方法,比如你提到的(SwingUtilities.invokeLater,SwingUtilities.invokeAndWait,...)。一个

My question is related to SwingUtilities.invokeLater. When should I use it? Do I have to use each time I need to update the GUI components? What does it exactly do? Is there an alternative to it since it doesn't sound intuitive and adds seemingly unnecessary code?

解决方案

Do I have to use each time I need to update the GUI components?

No, not if you're already on the event dispatch thread (EDT) which is always the case when responding to user initiated events such as clicks and selections. (The actionPerformed methods etc, are always called by the EDT.)

If you're not on the EDT however and want to do GUI updates (if you want to update the GUI from some timer thread, or from some network thread etc), you'll have to schedule the update to be performed by the EDT. That's what this method is for.

Swing is basically thread unsafe. I.e., all interaction with that API needs to be performed on a single thread (the EDT). If you need to do GUI updates from another thread (timer thread, networking thread, ...) you need to use methods such as the one you mentioned (SwingUtilities.invokeLater, SwingUtilities.invokeAndWait, ...).

这篇关于SwingUtilities.invokeLater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆