最快捷的方式来创建Java消息对话框(swing / awt / other)? [英] Fastest way to create a Java message dialog (swing/awt/other)?

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

问题描述

我正在创建一个Java应用程序,它将进行一些处理,然后需要显示一个消息来给用户反馈。



但是,它似乎是非常慢的 - 花了两秒钟才能返回。



我将这个源码删除了,看起来很明显,这里是使用的代码:

 包SwingPlay; 

import javax.swing.JFrame;

public class Dialog
{

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

}

我从命令行:

  java -classpath。 SwingPlay.Dialog 

正如你所看到的 - 我只是创建一个JFrame,甚至不显示



如果它是相关的,这里是我的 java -version 输出:

  java版本1.6.0_11
Java(TM)SE运行时环境(build 1.6.0_11-b03)
Java HotSpot (TM)客户端虚拟机(构建11.0-b16,混合模式,共享)

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






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



更重要的是,我只想要一个简单的消息(GUI,而不是cmdline)显示没有延迟 - 任何人都可以提供一些代码来做到这一点?






更新:



有点背景可能有帮助: br />
我正在创建一个应用程序,它将有许多头(即不同的用户界面都使用相同的核心类来执行复杂的部分)。


我目前有一个纯粹的命令行头,工作正常 - 立即回复。

我也将有一个标准的应用程序与常规点&点击GUI,并且不要预见这一点的问题。

我正在进行的工作是这两者的混合体 - 它将从运行框(或类似的启动器)启动,可能是参数,并且只需要有效地响应状态消息,可以通过按键来解除。



后一个是问题的重点。



虽然我不反对使用我现有的命令行版本与shell脚本(虽然不认为这是必要的!),现有的答案似乎表明,事情并没有像我们一样快速为他人 - 一个例子对我来说需要1460ms,而70ms是一个显着的差异。

解决方案

延迟的原因是因为Java是一个解释语言,需要时间才能启动新的JVM(解释器)



实际创建该帧的时间少于几ms(在我的机器中约为70 ms) p>

如果这将在Java应用程序中使用,你d不用担心。这将几乎是即时的(您应该使用JDialog或JOptionPane)



如果这不会在Java应用程序中使用,并且2秒它太多我认为这太多了)你应该考虑另一个工具。



以下是我在代码中测量时间的方法:

  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));
}

}


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.

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

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

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)

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


So, first question: Why is it so slow?

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


Update:

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.

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.

解决方案

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

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

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 )

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.

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消息对话框(swing / awt / other)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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