相关疑难解决方法(0)

SwingUtilities中的invokeAndWait方法

请解释SwingUtilities中的invokeAndWait()方法.我无法理解这一点. 非常清楚地解释一下. 如果你尝试一个例子,那将是很有帮助的.

编辑添加@ noob扩展问题:

什么是不太清楚这个

这是一个修改过的用法示例:

import javax.swing.SwingUtilities;

public class InvokeAndWaitStuff 
{
    public static void main(String[] args)
    {
        final Runnable doHelloWorld = new Runnable() {
             public void run() {
                 System.out.println("Hello World on " + Thread.currentThread());
             }
         };

         Thread appThread = new Thread() {
             public void run() {
                 try {
                     SwingUtilities.invokeAndWait(doHelloWorld);
                 }
                 catch (Exception e) {
                     e.printStackTrace();
                 }
                 System.out.println("Finished on " + Thread.currentThread());
             }
         };
         appThread.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Hello World on Thread[AWT-EventQueue-0,6,main]
Finished on Thread[Thread-0,5,main]
Run Code Online (Sandbox Code Playgroud)

为什么这很重要?:

导致doHelloWorld.run()在AWT事件派发线程上同步执行.此调用将阻塞,直到处理完所有待处理的AWT事件,并且(然后)doHelloWorld.run()返回.当应用程序线程需要更新GUI时,应使用此方法. …

java swing

19
推荐指数
1
解决办法
3万
查看次数

标签 统计

java ×1

swing ×1