Java EventQueue。为什么一切都应该在invokelater方法中? [英] Java EventQueue. Why should everything be in invokelater method?

查看:117
本文介绍了Java EventQueue。为什么一切都应该在invokelater方法中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在阅读的书中,有多线程的GUI的每个例子都有类似的东西:

  public static void main(String [] args)抛出异常
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new SomeKindOfFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

(我的意思是EventQueue)。但是不是在主(EDT)线程中自动执行的代码吗?

解决方案

桌面GUI应用程序通常以这种方式工作。 gui有一个线程,其余应用有一个或多个线程。使用 EventQueue 指定哪个GUI线程应该从其他线程执行。


in the book that i'm reading, every example of GUI with multithreading has something like that:

public static void main(String[] args) throws Exception
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            JFrame frame = new SomeKindOfFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

(i mean EventQueue). but isn't the code automatically executed in the main (EDT) thread?

解决方案

Desktop GUI applications usually work in this way. There is one thread for gui and one or several threads for rest of application. Using EventQueue you specify what GUI thread should do from other threads.

这篇关于Java EventQueue。为什么一切都应该在invokelater方法中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆