問題描述
我很好奇為什么我們必須使用java.awt.EventQueue.invokeLater
來控制swing組件.
I am very curious why do we have to use java.awt.EventQueue.invokeLater
to control swing components.
為什么我們不能在普通線程中這樣做?幕后究竟發生了什么?從我注意到的情況來看,如果我有一個 JFrame
我可以從主線程將可見性設置為 true 或 false 而不會出現任何錯誤,而且它似乎確實有效.那么使用 java.awt.EventQueue.invokeLater
究竟能實現什么?我也完全知道我可以使用 SwingUtilities.invokeLater
但正如 解釋在這里,它們似乎是一回事.
Why can't we do that in normal thread? What exactly is going on behind the scenes? From what I have noticed if I have a JFrame
I can set visibility to true or false from the main thread without getting any errors, and it does seem to work. So what exactly do I achieve by using java.awt.EventQueue.invokeLater
? I am also fully aware that I can use SwingUtilities.invokeLater
but as explained here, they seem to be one and the same thing.
感謝任何人的解釋.希望這是一個有效的問題.
Thanks to anyone for their explanation. Hopefully this is a valid question.
回答 wumpz 問題我們可以創建一個jframe
to answer wumpz question We can create a jframe
JFrame frame = new JFrame("Hello world");
frame.setSize(new Dimension(300, 300));
frame.setPreferredSize(new Dimension(300, 300));
frame.setMaximumSize(new Dimension(300, 300));
frame.setMinimumSize(new Dimension(300, 300));
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
在創建它的同一線程上執行以下操作.
And on the same thread it was created do the following.
for (int i = 0; i < 34; i++)
{
System.out.println("Main thread setting to "+(!frame.isVisible()));
frame.setVisible(!frame.isVisible());
}
沒有抱怨.
推薦答案
完整的 Swing 處理在一個名為 EDT(Event Dispatching Thread) 的線程中完成.因此,如果您要在此線程中計算一些持久的計算,您將阻止 GUI.
The complete Swing processing is done in a thread called EDT (Event Dispatching Thread). Therefore you would block the GUI if you would compute some long lasting calculations within this thread.
這里的方法是在不同的線程中處理您的計算,以便您的 GUI 保持響應.最后,您想要更新您的 GUI,這必須在 EDT 內完成.現在 EventQueue.invokeLater
開始發揮作用.它在 Swings 事件列表的末尾發布一個事件(您的 Runnable
),并在處理完所有先前的 GUI 事件后進行處理.
The way to go here is to process your calculation within a different thread, so your GUI stays responsive. At the end you want to update your GUI, which have to be done within the EDT. Now EventQueue.invokeLater
comes into play. It posts an event (your Runnable
) at the end of Swings event list and is processed after all previous GUI events are processed.
這里也可以使用 EventQueue.invokeAndWait
.不同之處在于,您的計算線程會阻塞,直到您的 GUI 更新.所以很明顯,這不能在 EDT 中使用.
Also the usage of EventQueue.invokeAndWait
is possible here. The difference is, that your calculation thread blocks until your GUI is updated.
So it is obvious that this must not be used from the EDT.
小心不要從不同的線程更新您的 Swing GUI.在大多數情況下,這會產生一些奇怪的更新/刷新問題.
Be careful not to update your Swing GUI from a different thread. In most cases this produces some strange updating/refreshing issues.
仍然有 Java 代碼可以從主線程簡單地啟動 JFrame.這可能會導致問題,但不會阻止 Swing.大多數現代 IDE 現在創建類似這樣的東西來啟動 GUI:
Still there is Java code out there that starts a JFrame simple from the main thread. This could cause issues, but is not prevented from Swing. Most modern IDEs now create something like this to start the GUI:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
這篇關于java.awt.EventQueue.invokeLater 解釋的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!