何时何地调用 EventQueue.invokeLater() 方法 [英] When and Where to call EventQueue.invokeLater() method

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

问题描述

我对线程和 GUI 完全陌生,因此我不知道在哪里调用这个 EventQueue.invokeLater() 方法.

I'm totally fresh about threading and GUIs, therefore I couldn't figure out exactly where to call this EventQueue.invokeLater() method.

我应该在每个事件侦听器和其他东西中调用它吗?调用这个方法的那些东西"是什么?如果是这样,是否有任何替代方法可以调用一次应用无处不在的方法,以便它不会花费大量的行将它们塞到事件调度线程中?

Am I supposed to call it in every event listeners and something else? What are those "things" to call this method? If so, is there any alternative way to call-once-apply-everywhere method so that It won't take bunch of lines to tuck them to the Event dispatch thread?

谢谢.

推荐答案

因此,我无法确定在何处调用此 EventQueue.invokeLater() 方法.

therefore I couldn't figure out exactly where to call this EventQueue.invokeLater() method.

Swing 组件需要在 EDT 上更新,因此如果您的代码在单独的线程中执行并且您想要更新 GUI 组件,那么您只能使用 invokeLater(...).

Swing components need to be updated on the EDT so you would only use invokeLater(...) if you have code executing in a separate Thread and you want to update a GUI component.

阅读 Swing 教程中关于 并发 的部分,了解更多信息.

Read the section from the Swing tutorial on Concurrency for more information.

作为一般规则,除非您使用线程,否则您只需要在创建 GUI 时使用此方法.查看 FrameDemo"nofollow">如何制作框架 一个简单的例子来帮助你入门.

As a general rule, unless you are using Threads, you only need to use this method when you create your GUI. Take a look at the FrameDemo from the section in the Swing tutorial on How to Make Frames for a simple example to get you started.

我应该在每个事件监听器中调用它吗?

Am I supposed to call it in every event listeners?

不!

事件处理程序中的所有代码都已在事件调度线程 (EDT) 上执行,因此您无需调用此方法.

All code in an event handler already executes on the Event Dispatch Thread (EDT)so you don't need to invoke this method.

这篇关于何时何地调用 EventQueue.invokeLater() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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