创建Java消息对话框最快的方法(秋千/ AWT /其他)? [英] Fastest way to create a Java message dialog (swing/awt/other)?

查看:138
本文介绍了创建Java消息对话框最快的方法(秋千/ AWT /其他)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Java应用程序,会做一些再处理需要显示一条消息给用户的反馈意见。

I'm creating a Java application that will do some processing then needs to display a message to give the user feedback.

不过,这似乎是慢得令人难以置信 - 接管两秒钟可返回

However, it appears to be incredibly slow - taking over two seconds to return.

我剥源到明显的罪魁祸首,而这里是用code:

I stripped the source down to the apparent culprit, and here is the code used:

package SwingPlay;

import javax.swing.JFrame;

public class Dialog
{

    public static void main( String[] args )
    {
        JFrame frame = new JFrame( "DialogDemo" );
    }

}

我在命令行中使用执行此:

I'm executing this from the command line with:

java -classpath . SwingPlay.Dialog

正如你所看到 - 我在做什么,但建立一个JFrame,甚至没有显示它

As you can see - I'm doing nothing but create a JFrame, not even displaying it.

在情况下,它是相关的,这里是我的 Java的版本输出:

In case it is relevant, here is my java -version output:

java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

这是(目前)对Win XP SP2的运行。

And this is (currently) running against Win XP SP2.


所以,第一个问题:为什么这么慢

So, first question: Why is it so slow?

更重要的是,我只想要一个简单的信息(GUI,没有CMDLINE)毫不拖延地显示 - ?任何人都可以提供一些code做到这一点。

More importantly, I just want a simple message (GUI, not cmdline) to be displayed without delay - can anyone provide some code to do this?


更新:

一点背景知识可能会有所帮助:

我创建一个应用程序,这将有许多头(即不同的用户界面都使用相同的核心类做复杂的零件)。


我现在有一个纯命令行头的正常工作 - 马上响应。

我也将有一个正规点与放一个标准的应用;单击图形用户界面,并没有预见与该位的问题。

什么我目前工作的是这两者的混合体 - 它会从运行框中(或类似的发射)推出,可能与参数,只需要使用,有效,状态响应消息,可与一键preSS驳回。

A bit of background might be helpful:
I am creating an application which will have many 'heads' (i.e. different user interfaces all using the same core classes to do the complex parts).
I currently have a pure command line head which works fine - responds straight away.
I will also have a standard application with a regular point & click GUI, and don't foresee problems with this bit.
What I am currently working on is a hybrid of these two - it will be launched from a Run box (or similar launcher), possibly with arguments, and only needs to respond with, effectively, a status message, that can be dismissed with a key press.

这后者是那里的问题集中。

This latter one is where the question is focused.

虽然我不反对使用现有的命令行版本的shell脚本(不过没想到这将是必要的!),现有的答案似乎表明,事情没有那么快,对我来说,他们是运行其他 - 一个例子需要1460ms对我来说,与70ms的 - 一个显著差异

Whilst I am not opposed to using my existing command line version with shell scripts (though didn't think it would be necessary!), the existing answers seem to suggest that things are not running as fast for me as they are for others - one example takes 1460ms for me, versus 70ms - a significant difference.

推荐答案

的原因延迟,因为Java是一个跨preTED语言,它需要时间来启动一个新的JVM(跨preTER)

The reason for the delay it because Java is an interpreted language and it takes time to start a new JVM ( the interpreter )

实际创建框架需要不到一数毫秒(在我的机器约70毫秒)。

Actually creating the frame takes less than a few ms ( about 70 ms in my machine ).

如果这将是一个Java应用程序中使用,你不必担心。这将是几乎在瞬间(你应该使用的JDialog或JOptionPane的这个)

If this is going to be used within a Java app, you don't need to worry about it. It will be almost instantaneous ( you should use JDialog or JOptionPane for this )

如果这是不会Java应用程序内使用,以及2秒太多(我认为这是太多了),你应该考虑这个工作的另一种工具。

If this is NOT going to be used inside a Java app, and 2 secs it too much ( and I think it is too much ) you should consider another tool for the job.

下面就是我如何测量时间在你code:

Here's how I measure the time in your code:

import javax.swing.JFrame;

public class Dialog {

    public static void main( String[] args ) {
        long start = System.currentTimeMillis();
        JFrame frame = new JFrame( "DialogDemo" );
        System.out.println( "Took: " + (  System.currentTimeMillis() - start   ) );
    }

}

这篇关于创建Java消息对话框最快的方法(秋千/ AWT /其他)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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