开发者

Why doesn't this trivial Swing-based program terminate?

开发者 https://www.devze.com 2023-01-07 06:58 出处:网络
Why doesn\'t this Swing-based program terminate when its window is closed? import javax.swing.JFrame; import javax.swing.JOptionPane;

Why doesn't this Swing-based program terminate when its window is closed?

import javax.swing.JFrame;
import javax.swing.JOptionPane;

final class App extends JFrame {
    开发者_开发技巧private App() {
        super("App");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JOptionPane.showMessageDialog(this, "App works");
        pack();
    }

    public static void main(final String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new App().setVisible(true);
            }
        });
    }
}


It doesn't terminate on anyone's system, first of all.

The reason is because you are calling a JFrame to be set visible with zero contents. It's most likely hiding very very small in the upper left hand corner of your screen. If you close that frame your program will terminate. The message dialog has nothing to do with the JFrame.


It does... what happens (on my machine) is that it opens up a dialog box. Once you hit "OK" on that dialog box it open up the JFrame. When you close the JFrame it terminates the app.

Yoy probably don't want to be doing the work you are doing inside of the constructor... then the JFrame will show up before the dialog box.

0

精彩评论

暂无评论...
验证码 换一张
取 消