应该从事件调度程序或主线程控制Swing GUI应用程序吗? [英] Should Swing GUI application be controlled from Event Dispatcher or main thread?

查看:112
本文介绍了应该从事件调度程序或主线程控制Swing GUI应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几本关于Java的书。在所有这些中,至少有一章教授GUI编程。在所有这些中,创建一个简单的表单应用程序遵循以下逻辑:

I've read a few books about Java. In all of them there was at least one chapter teaching GUI programming. In all of them, creating a simple form application was following this logic:

MyFrame.java

MyFrame.java

public class MyFrame extends JFrame
{
    JButton button1;

    public MyFrame()
    {
        button1 = new JButton("Click here.");
    }
}

FrameTest.java:

FrameTest.java:

public class FrameTest
{
    public static void main(String[] args)
    {
        MyFrame myFrame = new MyFrame();

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(600, 600);
        myFrame.setVisible(true);
    }
}

基本上,只需将JFrame子类化为创建表单并声明as instance变量其他组件并在构造函数中初始化这些组件。然后创建另一个测试类,在该类中实例化框架子类并调用它的一些方法使其可见。

Basically, just subclass JFrame to create a form and declare as instance variables the other components and initialize those components in the constructor. And then create another test class, instantiate the frame subclass in that class and call some of its methods to make it visible.

但在并发课程中我了解到我们有一个主线程在我们创建的每个应用程序中运行main()。据我所知,当我们使用Swing创建GUI应用程序时,我们有另一个线程(Event Dispatcher Thread)。因此,如果我没有记错,在每个基于Swing的GUI应用程序中至少有两个线程。这使得每个GUI应用程序都是多线程的。在我读过的一些文章和教程中,它说Swing不支持多线程,因此只能在Event Dispatcher Thread中创建和修改所有GUI组件,否则可能会出现线程干扰和内存不一致错误。

But in concurrency lessons I've learned that we have a main thread which runs main() in every application we create. As far as I know, when we use Swing in order to create GUI applications we have another thread (Event Dispatcher Thread). So if i am not mistaken, in every Swing based GUI application there are at least two threads. This makes every GUI application multithreaded. And in some articles and tutorials that I've read, it says that Swing doesn't support multithreading therefore all GUI components should be created and modified only in Event Dispatcher Thread otherwise Thread Interference and Memory Inconsistency Errors may arise.

即使是维基百科中最简单的例子( http:/ /en.wikipedia.org/wiki/Swing_%28Java%29 ),它是通过invokeLater方法制作的。

Even in the simplest example in Wikipedia (http://en.wikipedia.org/wiki/Swing_%28Java%29), it's made like this via invokeLater method.

那么哪一个是真正的方法?我错在哪里?

So which one is the true approach? Where am I wrong?

推荐答案

任何UI / Swing组件的所有交互都必须在EDT的上下文中完成

ALL interactions with any UI/Swing component MUST be done for within the context of the EDT

启动应用程序时,在尝试创建/与任何Swing组件交互之前,应确保在EDT中执行。

When starting an application, you should ensure that you are executing within the EDT BEFORE you try and create/interact with any Swing component.

简单地说,你应该使用类似的东西......

Simply put, you should use something like...

EventQueue.invokeLater(new Runnable() {
    public void run() {
        // Now in the event dispatching thread
    }
});

如果您需要运行长时间运行的任务或执行任何阻止任务,您应该单独执行它线。 SwingWorker 在大多数情况下是一个不错的选择,因为它提供了一些简单的机制来将代码重新同步到事件调度线程。

If you need to run long running task or perform any blocking task, you should execute it in a separate thread. SwingWorker is a good choice in most cases as it provides some simple mechanisms for re-syncing code to the event dispatching thread.

读一读

  • Initial Thread
  • Will the real Swing Single Threading Rule please stand up?

所以,简短的回答是,是的,所有基于Swing的代码都应该从EDT的上下文中访问/修改/交互

So, the short answer is, yes, all Swing based code should be accessed/modified/interacted with from the context of the EDT

这篇关于应该从事件调度程序或主线程控制Swing GUI应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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