- java.lang.Object
-
- java.awt.EventQueue
-
public class EventQueue extends Object
EventQueue是一个独立于平台的类,可以从基础对等体类和可信应用程序类排队事件。它封装了从队列中提取事件,并通过调用调度它们异步事件指派机制
dispatchEvent(AWTEvent)这种方法EventQueue与该事件被分派作为参数。 这种机器的特殊行为是依赖于实现的。 唯一的要求是实际排队到这个队列的事件(注意,发布到EventQueue事件可以合并)被调度:- 顺序。
- 也就是说,不允许同时调度此队列中的几个事件。
- 它们排列顺序相同。
-
也就是说,如果
AWTEventA被排入到EventQueue前AWTEventB,则事件B不能在事件A之前被指派
一些浏览器将不同代码库中的小程序分割成单独的上下文,并在这些上下文之间建立墙壁。 在这种情况下,每个上下文将有一个
EventQueue。 其他浏览器将所有小程序放入相同的上下文中,这意味着所有小程序将只有一个全局的EventQueue。 这种行为是依赖于实现的。 有关更多信息,请参阅浏览器的文档。有关事件调度机械线程问题的信息,请参阅AWT Threading Issues 。
- 从以下版本开始:
- 1.1
-
-
构造方法摘要
构造方法 Constructor 描述 EventQueue()初始化一个新的实例EventQueue。
-
方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 描述 SecondaryLoopcreateSecondaryLoop()创建与此事件队列相关联的新的secondary loop。protected voiddispatchEvent(AWTEvent event)调度事件。static AWTEventgetCurrentEvent()返回与调用线程相关联的EventQueue当前调度的事件。static longgetMostRecentEventTime()返回具有时间戳记的最新事件的时间戳,并从与调用线程相关联的EventQueue中调度。AWTEventgetNextEvent()从EventQueue删除事件并返回。static voidinvokeAndWait(Runnable runnable)static voidinvokeLater(Runnable runnable)static booleanisDispatchThread()如果调用线程是the current AWT EventQueue的分派线程,则返回true。AWTEventpeekEvent()返回EventQueue上的第一个事件,而不删除它。AWTEventpeekEvent(int id)返回具有指定ID的第一个事件(如果有)。protected voidpop()停止派遣事件使用这EventQueue。voidpostEvent(AWTEvent theEvent)发布一个1.1风格的事件到EventQueue。voidpush(EventQueue newEventQueue)用指定的替换现有的EventQueue。
-
-
-
方法详细信息
-
postEvent
public void postEvent(AWTEvent theEvent)
发布一个1.1样式的事件到EventQueue。 如果具有相同ID和事件源的队列上存在现有事件,则将调用源Component的coalesceEvents方法。- 参数
-
theEvent-实例java.awt.AWTEvent,或它的子类 - 异常
-
NullPointerException- 如果theEvent是null
-
getNextEvent
public AWTEvent getNextEvent() throws InterruptedException
从EventQueue删除事件并返回。 此方法将阻止,直到事件已被另一个线程发布。- 结果
-
下一个
AWTEvent - 异常
-
InterruptedException- 如果任何线程已经中断了此线程
-
peekEvent
public AWTEvent peekEvent()
返回EventQueue上的第一个事件,而不删除它。- 结果
- 第一个事件
-
peekEvent
public AWTEvent peekEvent(int id)
返回具有指定ID的第一个事件(如果有)。- 参数
-
id- 所需事件类型的id - 结果
-
指定id的第一个事件或
null如果没有这样的事件
-
dispatchEvent
protected void dispatchEvent(AWTEvent event)
调度事件。 事件的分派方式取决于事件的类型和事件的源对象的类型: Event types, source types, and dispatch methods Event Type Source Type Dispatched To ActiveEvent Any event.dispatch() Other Component source.dispatchEvent(AWTEvent) Other MenuComponent source.dispatchEvent(AWTEvent) Other Other No action (ignored)- 参数
-
event-实例java.awt.AWTEvent,或它的子类 - 异常
-
NullPointerException- 如果event是null - 从以下版本开始:
- 1.2
-
getMostRecentEventTime
public static long getMostRecentEventTime()
返回具有时间戳的最新事件的时间戳,并从与调用线程相关联的EventQueue中分派。 如果当前正在调度具有时间戳的事件,则会返回其时间戳。 如果尚未调度事件,则将返回EventQueue的初始化时间。在当前版本的JDK中,只有InputEvents,ActionEvents和InvocationEvent具有时间戳; 然而,JDK的未来版本可能会向其他事件类型添加时间戳。 请注意,此方法只能从应用程序的event dispatching thread调用。 如果从另一个线程调用此方法,则将返回当前系统时间(由System.currentTimeMillis()报告)。- 结果
-
的时间戳最后
InputEvent,ActionEvent,或InvocationEvent被分派,或System.currentTimeMillis()如果该方法在不同于事件指派线程的线程上调用 - 从以下版本开始:
- 1.4
- 另请参见:
-
InputEvent.getWhen(),ActionEvent.getWhen(),InvocationEvent.getWhen(),isDispatchThread()
-
getCurrentEvent
public static AWTEvent getCurrentEvent()
返回与调用线程相关联的EventQueue当前调度的事件。 如果一个方法需要对该事件的访问,但是这个方法没有被设计为接受对它的引用作为参数,这是有用的。 请注意,此方法只能从应用程序的事件调度线程调用。 如果从另一个线程调用此方法,则返回null。- 结果
- 当前正在调度的事件,如果在除事件分派线程之外的线程上调用此方法,则为null
- 从以下版本开始:
- 1.4
-
push
public void push(EventQueue newEventQueue)
用指定的替换现有的EventQueue。 任何待处理的事件都将转移到新的EventQueue进行处理。- 参数
-
newEventQueue- 要使用的EventQueue(或其子类)实例 - 异常
-
NullPointerException- 如果newEventQueue是null - 从以下版本开始:
- 1.2
- 另请参见:
-
pop()
-
pop
protected void pop() throws EmptyStackException停止使用这个EventQueue调度事件。 任何待处理的事件都将转移到先前的EventQueue进行处理。警告:为避免死锁,请勿在子类中声明此方法同步。
- 异常
-
EmptyStackException- 如果以前没有推出这个EventQueue - 从以下版本开始:
- 1.2
- 另请参见:
-
push(java.awt.EventQueue)
-
createSecondaryLoop
public SecondaryLoop createSecondaryLoop()
创建与此事件队列相关联的新的secondary loop。 使用SecondaryLoop.enter()和SecondaryLoop.exit()方法启动和停止事件循环并从此队列中分派事件。- 结果
- secondaryLoop一个新的辅助循环对象,可用于启动新的嵌套事件循环并从此队列调度事件
- 从以下版本开始:
- 1.7
- 另请参见:
-
SecondaryLoop.enter(),SecondaryLoop.exit()
-
isDispatchThread
public static boolean isDispatchThread()
如果调用线程是the current AWT EventQueue的分派线程,则返回true。 使用此方法确保某个特定任务正在执行(或不在)。注意:使用
invokeLater(java.lang.Runnable)或invokeAndWait(java.lang.Runnable)方法在the current AWT EventQueue的发送线程中执行任务。- 结果
-
如果在
the current AWT EventQueue的发送线程中运行, 则为 true - 从以下版本开始:
- 1.2
- 另请参见:
-
invokeLater(java.lang.Runnable),invokeAndWait(java.lang.Runnable),Toolkit.getSystemEventQueue()
-
invokeLater
public static void invokeLater(Runnable runnable)
导致runnable在dispatch thread的dispatch thread中调用其run方法。 这将在所有待处理的事件被处理之后发生。- 参数
-
runnable-Runnable其run方法应该在event dispatch thread中异步执行the system EventQueue - 从以下版本开始:
- 1.2
- 另请参见:
-
invokeAndWait(java.lang.Runnable),Toolkit.getSystemEventQueue(),isDispatchThread()
-
invokeAndWait
public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException
导致runnable要求其run方法在dispatch thread的the system EventQueue中调用 。 这将在所有待处理的事件被处理之后发生。 调用块直到发生这种情况。 如果从event dispatcher thread调用,此方法将抛出错误。- 参数
-
runnable-Runnable其run方法应在event dispatch thread中同步执行event dispatch thread的the system EventQueue - 异常
-
InterruptedException- 如果任何线程中断了此线程 -
InvocationTargetException- 如果在运行时抛出一个throwablerunnable - 从以下版本开始:
- 1.2
- 另请参见:
-
invokeLater(java.lang.Runnable),Toolkit.getSystemEventQueue(),isDispatchThread()
-
-