为什么我的样板Java桌面应用程序JFrame在main方法中使用EventQueue.invokeLater? [英] Why does my boilerplate Java desktop app JFrame use EventQueue.invokeLater in the main method?

查看:55
本文介绍了为什么我的样板Java桌面应用程序JFrame在main方法中使用EventQueue.invokeLater?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的Eclipse和GWT Designer来制作Java中的swing应用程序.工具自动生成的应用程序窗口中的主要功能(这是一个javax.swing.JFrame)如下:

I am using the latest Eclipse, and GWT Designer, to make a swing application in Java. The main function in my application window (which is a javax.swing.JFrame) in the auto generated by the tools looks like this:

    /* launch the application */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                AppWindow window = new AppWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

似乎本来就是这样的声音:

This seems like a lot of noise around what could have been just this:

public static void main(String[] args) {
            try {
                AppWindow window = new AppWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }


}

我已经了解到在某些情况下需要使用EventQueue.InvokeLater技术,另一个问题询问在哪里使用它

I have read that the EventQueue.InvokeLater technique is required in some situations, and another question asks where to use it here.

我的问题比较简单;为什么在这里的代码生成器中自动执行此操作?为什么main应该快速返回并让事件队列稍后创建应用程序窗口?难道就不是重点吗?为什么JFrame自动生成的设计器会执行此EventQueue任务?我尝试过看看在启动和显示表格方面是否有所不同,无论此代码是用简单的方法还是用困难的方法完成,我只能暂时得出结论,它具有一些好处,这些好处在初学者制作的小型演示应用程序中是不可见的像我一样,也许在现实世界中基于大型复杂Jframe的类中,这种延迟/排队策略有一些好处?

My question is simpler; Why do this automatically in the code generator here? Why should main return quickly and let the application window get created later by the event queue? Wouldn't blocking be exactly the point? Why is the JFrame auto-generated designer doing this EventQueue stuff? I have tried to see some difference in the start up and showing of forms whether this code is done the simpler way or the harder way, and I can only conclude provisionally that this has some benefits that are not visible in tiny demo apps made by beginners like me, and that perhaps in real-world large complex Jframe based classes, there is some benefit to this delaying/queuing strategy?

推荐答案

根据您的应用程序及其使用方式,可能会在屏幕上绘制某些内容(并因此使用 EventQueue ),然后调用 main 方法.应当在 Event Dispatch Thread 上进行修改任何UI组件的调用,这包括将应用程序设置为可见.

Depending on your application and how it's being used, it's possible that there could be something that is drawing on the screen (and thus using the EventQueue) before or during the call to your main method. Calls that modify any UI components should be made on the Event Dispatch Thread, and this includes setting the application visible.

为了安全起见,在 EDT 上启动应用程序是一个好习惯.

So just to be safe, it's a good practice to start your application on the EDT.

为什么在这里的代码生成器中会自动执行此操作?

Why do this automatically in the code generator here?

它不会造成伤害,易于生成,并且被认为是很好的做法.

It won't hurt, it's easy to generate, and it's considered good practice.

为什么主程序应该快速返回并让事件队列稍后创建应用程序窗口?

Why should main return quickly and let the application window get created later by the event queue?

可能正在从其他使用 EDT 的应用程序中调用 main 方法,并且可能已经在屏幕上绘制了一些内容.如果直接在 main 中绘制应用程序,则可能是您的应用程序正在更改正在由 EDT 上的某些内容处理的某些组件,并且可能已经在屏幕上绘制.

It's possible that the main method is being called from some other application that is using the EDT and may have already drawn something on screen. If you draw your application directly in main, it's possible that your application may be altering some component that is in the process of being handled by something on the EDT, and potentially already drawn on the screen.

因此,为了安全起见,应将其保留在 EDT 上以绘制您的应用程序,以便它可以在不干扰其他任何内容的情况下进行操作./p>

So just to be safe in case this situation ever happens, you should leave it up to the EDT to draw your application so it can do it when it won't interfere with anything else.

难道不是要说清楚吗?

Wouldn't blocking be exactly the point?

除非您的用户通过双击桌面图标启动的JVM进程以外的其他调用方式是调用 main ,否则当 main 返回时,不会有什么不同只要屏幕上有东西.

Unless something else is calling main other than the JVM process that your user started by double-clicking the desktop icon, it's not going to make a difference when main returns as long as there is something on the screen.

我只能暂时得出结论,它具有一些好处,这些好处在像我这样的初学者制作的微型演示应用程序中是看不到的

I can only conclude provisionally that this has some benefits that are not visible in tiny demo apps made by beginners like me

您是对的-在大多数情况下,它可能不会有所作为,但我认为他们包括了它,因为它很容易生成&实施,它不会造成伤害,并且将是良好实践的例证.

You're right - most of the time it's probably not gonna make a difference, but I presume they included it because it was easy to generate & implement, it can't hurt, and it would exemplify good practice.

这篇关于为什么我的样板Java桌面应用程序JFrame在main方法中使用EventQueue.invokeLater?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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