Could someone explain what purpose EventQueue object serves?

雨燕双飞 提交于 2019-12-12 03:04:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!