创建 Java 消息对话框的最快方法(swing/awt/other)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/508723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest way to create a Java message dialog (swing/awt/other)?
提问by Peter Boughton
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 -versionoutput:
如果相关,这是我的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' (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.
一些背景知识可能会有所帮助:
我正在创建一个应用程序,它将有许多“头”(即不同的用户界面都使用相同的核心类来完成复杂的部分)。
我目前有一个工作正常的纯命令行头 - 立即响应。
我还将拥有一个带有常规点击式 GUI 的标准应用程序,并且没有预见到这一点的问题。
我目前正在研究的是这两者的混合 - 它将从运行框(或类似的启动器)启动,可能带有参数,并且只需要有效地响应状态消息,可以用按键。
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 毫秒 - 一个显着的差异。
回答by OscarRyz
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. It will be almost instantaneous ( you should use JDialog or JOptionPane for this )
如果这将在 Java 应用程序中使用,您无需担心。它几乎是即时的(您应该为此使用 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 ) );
}
}
回答by Jason Day
I would use a JOptionPaneto show the message. Here's a simple example:
我会使用JOptionPane来显示消息。这是一个简单的例子:
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 毫秒内运行。
回答by Michael Borgwardt
Java is the wrong tool for this. 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.
Java 是错误的工具。设置 JVM 涉及在执行第一行 Java 代码之前在后台发生的很多事情,而且真的没有办法绕过它。
回答by John Gardner
Do you NEED to use java to display the message box? IF the box is coming from outside of your application, then you might want to use something else to generate a dialog.
你需要使用java来显示消息框吗?如果框来自您的应用程序外部,那么您可能需要使用其他东西来生成对话框。
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. 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:
制作一个只显示来自命令行字符串的消息框的本机 Windows 应用程序最多只需要几个小时。大多数常见的脚本语言也应该有办法做到这一点。这是一些人通过命令行通过 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
回答by Bombe
回答by Bombe
回答by ShawnD
You could use the JOptionDialog
你可以使用 JOptionDialog
JOptionPane.showMessageDialog([parent frame], [message], [title], JOptionPane.MESSAGE_TYPE);
回答by Sudarshan S
Since you're interested in speeding this up, and since most of the overhead seems to be JVM startup overhead, check out Nailgunwhich aims to address slow JVM startup by keeping a JVM running in the background all the time. 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.
由于您有兴趣加快速度,而且大部分开销似乎是 JVM 启动开销,请查看Nailgun,它旨在通过让 JVM 始终在后台运行来解决 JVM 启动缓慢的问题。在您的情况下,在一次运行后,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,这种方法会导致内存使用量增加,并且还会导致其他问题,因为确定何时关闭它可能并不简单。
回答by Michael Myers
Have you tried running it through a profiler like NetBeans? If there's a bottleneck deep inside the standard library, that's a good way to find it.
您是否尝试过通过NetBeans 之类的分析器运行它?如果在标准库内部存在瓶颈,这是找到它的好方法。
回答by Stephane Grenier
What you're probably looking for is the new SplashScreen functionality in Java 6. 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.
您可能正在寻找的是Java 6 中新的 SplashScreen 功能。不必等待 JVM 加载(加载任何 VM 总是有成本的),这将预先加载一个屏幕。

