Java 事件队列。我什么时候应该考虑使用它?
我目前正在Oracle网站上查看EventQueue类: http://download.oracle.com/javase/1.4.2/文档/api/java/awt/EventQueue.html 但我不确定什么时候应该使用它?如果我的类有两个或多个事件的侦听器,我应该使用它吗?
I'm currently looking at the EventQueue class on the Oracle website:
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/EventQueue.html
But I'm not sure when I should use it? Should I use it if my class has listeners for two or more Events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,您不必向 EventQueue 提交任何事件,当用户执行操作(如鼠标单击等)时,或者当系统认为您的窗口需要重新绘制时,这一切都会“自动”发生。
我经常使用的唯一两个方法是
EventQueue.invokeLater
和EventQueue.invokeAndWait()
(较少使用)。如果您要在 EDT(事件调度线程)之外执行某些操作,然后想要对 GUI 进行一些更改(例如向容器添加或从容器中删除组件),请使用其中之一,因为此类操作应仅发生在美国东部时间。Normally you don't have to submit any events to the EventQueue, this all happens "automatically" when the user does his actions (like mouse clicks and such), or when the system thinks your window needs to be repainted.
The only two methods I'm using regularly are
EventQueue.invokeLater
andEventQueue.invokeAndWait()
(less often). Use one of them if you are doing some action outside of the EDT (event dispatch thread) and then want to do some changes to the GUI (like adding or removing a component to/from a container), as such actions should occur only on the EDT.我在 14 年的 Java 编程中从未使用过它。
I've never used it in 14 years of Java programming.
AWT 使用它们来处理 GUI 的底层事务。通常您不会使用它们,除非您在 AWT 之上构建一些 GUI 引擎。就像Paulo所说,有一个重要的方法
invokeLater
,但通常你可以使用SwingUtilities.invokeLater
达到相同的效果,这种方式似乎更常用。AWT uses these to take care of things for your GUI under the hood. Normally you wouldn't use these it unless you are building some GUI engine on top of AWT. Like Paulo said there is this important method
invokeLater
but usually you can achieve the same effect usingSwingUtilities.invokeLater
this way seems to be used lot more often.