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

查看:22
本文介绍了为什么人们在事件队列上运行 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.

为什么人们在 EDT 上运行 Java GUI?

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,这些 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).

      EDIT您询问了如何在 EDT 之外运行冗长操作的示例.有几种方法可以做到这一点.在最简单的情况下,您只需从 EDT 创建并启动一个新线程.下面是一个例子:当用户点击一个按钮时,监听器回调将被调用,我们知道这将发生在 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天全站免登陆