处理事件指派线程 [英] Handling the Event Dispatch Thread

查看:180
本文介绍了处理事件指派线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于事件指派线程的问题。我有一个主类,这也是一个JFrame。它初始化在code中的其他组件,他们中的一些不涉及Swing和他们中的一些事情。是否足以只是初始化使用EDT这样的主类?...

I have a question about the 'Event Dispatch Thread'. I have a Main class that is also a JFrame. It initialises the rest of the components in the code, some of them do not involve Swing and some of them do. Is it enough to simply initialise the Main class using the EDT like this?...

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Main();
        }
    });
}

这一切的方式将事件调度线程上运行。

This way everything would run on the Event Dispatcher thread.

推荐答案

这是直到你开始利用后台线程进行计算,数据采集等,然后你需要开始小心,以验证您是在通常就足够了在EDT之前改变Swing组件或它的模式。

That is generally sufficient until you start making use of background threads for calculations, data acquisition, etc. Then you need to start being careful to verify that you are on the EDT prior to altering a swing component or its model.

无论你是在美国东部时间与执行,您可以测试:

You can test whether you're executing on the EDT with:

    if (SwingUtilities.isEventDispatchThread()) {
        // Yes, manipulate swing components
    } else {
        // No, use invokeLater() to schedule work on the EDT
    }

此外,请参阅的SwingWorker 类的细节如何工作移交给后台线程和处理结果在EDT

Also, see the SwingWorker class for details on how to hand off work to a background thread and process results on the EDT

这篇关于处理事件指派线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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