[英]SwingUtilities.invokeLater()
在任何swing应用程序中,我如何感觉到SwingUtilities.invokeLater()的重要性。请提供一些代码示例。
Whenever you need to update something inside your GUI you should do it through the AWT Event Thread . 每当需要在GUI中进行某些更新时,都应通过AWT事件线程进行更新 。
This because AWT (and Swing on top) has its own thread that manages everything of a GUI. 这是因为AWT(位于顶部的Swing)具有自己的线程,该线程管理GUI的所有内容。 Without it the graphical interface couldn't handle events and similar things in an asynchronous way while your program is doing something else.
没有它,图形界面将无法在程序执行其他操作时以异步方式处理事件和类似内容。
So for example if you have a long task declared in a Thread
: 因此,例如,如果您在
Thread
声明了一个长任务:
public void MyThread extends Thread
{
class GUIUpdate implements Runnable
{
GUIUpdate(String msg)
{
...
}
public void run()
{
guiElement.appendText(msg);
}
}
public void run()
{
while (finished)
{
//do long calculations
//send partial output to gui
SwingUtilities.invokeLater(new GUIUpdate("something has changed!"));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.