为什么人们在事件队列上运行Java GUI [英] Why do people run Java GUI's on the Event Queue

查看:140
本文介绍了为什么人们在事件队列上运行Java GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,要创建并显示一个新的 JFrame ,我只需这样做:

In Java, to create and show a new JFrame, I simply do this:

public static void main(String[] args) {
   new MyCustomFrameClass().setVisible(true);
}

但是,我见过很多人这样做:

However, I have seen many people doing it like this:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MyCustomFrameClass().setVisible(true);
       }
    });
}

为什么?是否有任何优势?

Why? Are there any advantages?

推荐答案

管理需要在EDT上执行的操作的规则(我更常使用EDT)比事件队列)在Java的生命周期中发生了变化。每当规则发生变化时,Sun建议在EDT上做越来越多的GUI相关工作。

The rules governing what needs to be performed on the EDT (I see "EDT" more often used than "Event Queue") have changed during Java's lifetime. And everytime the "rules" changed, Sun advised doing more and more "GUI related" work on the EDT.


为什么人们运行Java美国东部时间的图形用户界面?

Why do people run Java GUI’s on the EDT?




  • 因为官方指南建议这样做。

    • Because the official guidelines advise doing so.

      因为这样做有助于避免许多与GUI相关的线程错误。

      Because doing so will help dodge a lot of threading bugs related to the GUI.

      注意,这并不是很清楚,EDT实际上偶尔会发生崩溃,因为Swing本身有一些错误。每个非平凡的Swing应用程序都使用具有错误的Swing API,因此EDT会在一段时间内死掉。

      Note, and this is not very well known, that the EDT actually does crash once in a while, because Swing itself has a few bugs. Every non-trivial Swing application is using Swing APIs that have, well, bugs and hence once in a while the EDT dies.

      你永远不会看到它而它不是令人担忧的是,因为当EDT死亡时,它会自动重启。

      You never see it and it isn't a cause of concerns, because when the EDT dies it is automagically restarted.

      基本上,在EDT上做所有与GUI相关的事情,并在EDT之外做所有长时间的操作(至于不阻止EDT)。

      Basically, do all GUI-related stuff on the EDT and do all lenghty operations outside the EDT (as to not block the EDT).

      编辑你问了一个关于如何在EDT之外运行lenghty操作的例子。有几种方法可以做到这一点。在最简单的情况下,您只需从EDT创建并启动一个新的Thread。这是一个例子:当用户点击按钮时应该调用监听器回调,我们知道这将在EDT上发生......

      EDIT you asked for an example as to how to run a lenghty operation outside the EDT. There are several ways to do this. In the simplest case, you simply create and start a new Thread, from the EDT. Here's one example: the listener callback shall be called when the user clicks on a button, we know that this shall happen on the EDT...

          JButton jb = ...
          jb.addActionListener( new ActionListener() {
              public void actionPerformed( final ActionEvent e ) {
                final Thread t = new Thread( new Runnable() {
                 public void run() {
                   // this shall get executed, after start() has been called, outside the EDT    
                   }
                 });
                 t.start();
              }
          } );
      

      对于更复杂的示例,您希望阅读 SwingWorker 等。

      For more complicated examples, you want to read on SwingWorker, etc.

      这篇关于为什么人们在事件队列上运行Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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