问题
Could someone explain what purpose EventQueue
object serves in Java Swing?
I'm mostly confused about how queuing events is applied. Maybe providing common use of an event queue would be helpful.
回答1:
Because Swing is single threaded, it uses the EventQueue to hold instances of Runnable
so that they can be dispatched sequentially and in the order posted, whether generated by the system or your program. The sine qua non usage is posting an event via EventQueue.invokeLater()
, which ensures that Swing GUI objects are constructed and manipulated only on the event dispatch thread. You can replace the AWT EventQueue
with your own implementation, as shown here, to see the events as they are placed in the queue.
来源:https://stackoverflow.com/questions/36904354/could-someone-explain-what-purpose-eventqueue-object-serves