Swing event-handling and painting code executes in a single thread, called the event-dispatching thread.
This ensures that each event handler finishes executing before the next one executes and that painting isn't interrupted by events. To avoid the possibility of deadlock, you must take extreme care that Swing components and models are created, modified, and
queried only from the event-dispatching thread.
Note: We used to say that you could create the GUI on the main thread as long as you didn't modify components that had already been realized. [PENDING: The following red text belongs in a footnote.] Realized means that the component has been painted onscreen, or is ready to be painted. The methodssetVisible(true)andpackcause a window to be realized, which in turn causes the components it contains to be realized. While this worked for most applications, in certain situations it could cause problems. Out of all the demos in the Swing Tutorial, we encountered a problem only in ComponentEventDemo. In that case, sometimes when you launched the demo, it would not come up because it would deadlock when updating the text area if the text area had not yet been realized, while other times it would come up without incident.To avoid the possibility of thread problems, we recommend that you use
invokeLaterto create the GUI on the event-dispatching thread for all new applications. If you have old programs that are working fine they are probably OK; however you might want to convert them when it's convenient to do so.
Swing采用单一事件分发线程来处理所有事件及绘制操作,确保每个事件处理完毕才执行下一个,并避免绘制过程中被事件打断。为防止死锁,Swing组件及其模型必须仅从事件分发线程创建、修改和查询。推荐使用invokeLater方法在事件分派线程中创建GUI,以避免多线程问题。
965

被折叠的 条评论
为什么被折叠?



