简体   繁体   English

创建Java消息对话框的最快方法(swing / awt / other)?

[英]Fastest way to create a Java message dialog (swing/awt/other)?

I'm creating a Java application that will do some processing then needs to display a message to give the user feedback. 我正在创建一个Java应用程序,它将进行一些处理,然后需要显示一条消息以向用户提供反馈。

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. 正如你所看到的 - 我什么也没做,只是创建了一个JFrame,甚至没有显示它。

In case it is relevant, here is my java -version output: 如果它是相关的,这是我的java -version输出:

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. 这是(目前)针对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? 更重要的是,我只想要一个简单的消息(GUI,而不是cmdline)毫不拖延地显示 - 任何人都可以提供一些代码来执行此操作吗?


Update: 更新:

A bit of background might be helpful: 一些背景可能会有所帮助:
I am creating an application which will have many 'heads' (ie 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. 我还将有一个带有常规点击式GUI的标准应用程序,并且不会预见到这个问题。
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. 虽然我并不反对将现有的命令行版本与shell脚本一起使用(虽然不认为这是必要的!),现有的答案似乎表明事情对我来说并不像对别人那样快 - 一个例如,对我来说需要1460毫秒,而70毫秒 - 这是一个显着的差异。

The reason for the delay it because Java is an interpreted language and it takes time to start a new JVM ( the interpreter ) 延迟它的原因是因为Java是一种解释型语言并且启动一个新的JVM(解释器)需要时间

Actually creating the frame takes less than a few ms ( about 70 ms in my machine ). 实际上创建帧只需不到几毫秒(我的机器约70毫秒)。

If this is going to be used within a Java app, you don't need to worry about it. 如果要在Java应用程序中使用它,则无需担心它。 It will be almost instantaneous ( you should use JDialog or JOptionPane for this ) 它几乎是瞬间的(你应该使用JDialog或JOptionPane)

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. 如果这不是在Java应用程序中使用,并且2秒太多(我认为它太多)你应该考虑另一个工具。

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

}

I would use a JOptionPane to show the message. 我会使用JOptionPane来显示消息。 Here's a simple example: 这是一个简单的例子:

import javax.swing.*;

public class OptionDemo {
    public static void main(String[] args) throws Exception {
        JOptionPane.showMessageDialog(null, "Hello World");
    }
}

I'm afraid I can't explain the delay you're experiencing though. 我恐怕无法解释你遇到的延迟。 On my system, your code snippet runs in 500 milliseconds. 在我的系统上,您的代码段在500毫秒内运行。

Java is the wrong tool for this. Java是错误的工具。 Setting up the JVM involves a lot of stuff happening in the background before the first line of Java code can be executed, and there's really no way to get around it. 设置JVM涉及在第一行Java代码执行之前在后台发生的很多事情,而且实际上没有办法解决它。

创建一个AWT窗口 (或者一个框架 )而不是JFrame也会快得多,因为后者必须引入大量额外的类文件。

Do you NEED to use java to display the message box? 你需要使用java来显示消息框吗? IF the box is coming from outside of your application, then you might want to use something else to generate a dialog. 如果该框来自您的应用程序之外,那么您可能希望使用其他内容来生成对话框。

To make a native windows app that just shows a message box from a command line string would only take a few hours at most. 要创建一个仅显示命令行字符串消息框的本机Windows应用程序,最多只需要几个小时。 Most of the common scripting languages should have ways to do it too. 大多数常见的脚本语言也应该有办法。 here's an example from some guy through javascript via command line: 这是一些人通过命令行通过javascript的例子:

http://www.snee.com/bobdc.blog/2009/01/displaying-a-message-box-from.html http://www.snee.com/bobdc.blog/2009/01/displaying-a-message-box-from.html

Since you're interested in speeding this up, and since most of the overhead seems to be JVM startup overhead, check out Nailgun which aims to address slow JVM startup by keeping a JVM running in the background all the time. 由于您有兴趣加快速度,并且由于大部分开销似乎都是JVM启动开销,因此请查看Nailgun ,它旨在通过始终在后台运行JVM来解决慢速JVM启动问题。 In your case, after one run the Swing library too will end up being cached (and hopefully after a few more runs JITed too), further reducing the overhead. 在你的情况下,在一次运行之后,Swing库也将最终被缓存(并且希望在经过JIT之后再运行几次),进一步减少了开销。

However this approach will lead to increased memory usage due to the background JVM and also cause other problems since it may not be straightforward to determine when to shut it down. 但是,这种方法会因后台JVM而导致内存使用量增加,并且还会导致其他问题,因为可能无法直接确定何时关闭它。

哦,如果你真的不需要从Java显示对话框,你可以考虑使用KDialog (或它的GNOME对应物)或类似的东西。

您可以使用JOptionDialog

JOptionPane.showMessageDialog([parent frame], [message], [title], JOptionPane.MESSAGE_TYPE);

What you're probably looking for is the new SplashScreen functionality in Java 6 . 您可能正在寻找的是Java 6中新的SplashScreen功能 Instead of having to wait for the JVM to load (there's always a cost to load any VM), this will load a screen beforehand. 而不是必须等待JVM加载(加载任何VM总是有成本),这将预先加载一个屏幕。

Have you tried running it through a profiler like NetBeans ? 您是否尝试通过NetBeans等分析器运行它? If there's a bottleneck deep inside the standard library, that's a good way to find it. 如果标准库内部存在瓶颈,那么这是找到它的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM