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

后端 未结 10 841
醉梦人生
醉梦人生 2021-01-17 12:41

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 -

相关标签:
10条回答
  • 2021-01-17 12:56

    You could use the JOptionDialog

    JOptionPane.showMessageDialog([parent frame], [message], [title], JOptionPane.MESSAGE_TYPE);
    
    0 讨论(0)
  • 2021-01-17 12:57

    I would use a JOptionPane to show the message. 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.

    0 讨论(0)
  • 2021-01-17 13:04

    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.

    0 讨论(0)
  • 2021-01-17 13:05

    Oh, and if you don’t really need to show the dialog from Java you could look into using KDialog (or it’s GNOME counterpart) or something similar.

    0 讨论(0)
  • 2021-01-17 13:06

    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   ) );
        }
    
    }
    
    0 讨论(0)
  • 2021-01-17 13:09

    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.

    0 讨论(0)
提交回复
热议问题