当前位置: 代码迷 >> J2SE >> Swing,怎么关闭单个窗口,而不是退出整个程序
  详细解决方案

Swing,怎么关闭单个窗口,而不是退出整个程序

热度:103   发布时间:2016-04-24 12:19:07.0
Swing,如何关闭单个窗口,而不是退出整个程序?
一个程序打开了多个窗口,如何在关闭单个窗口的时候仅仅停止这个窗口以及其运行的程序而不是退出整个程序?
目前是用的
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
不过这个关闭窗口之后,所有的窗口都关闭了.

应该怎么做呢?
难道是用一个function来停止当前关闭窗口的程序,然后再设定setVisible(false)?

谢谢.

------解决方案--------------------
例子举的不好,但如果你明白了你就可以把它改好了,呵呵!
Java code
public class NewJFrame extends javax.swing.JFrame {    /**     * Auto-generated main method to display this JFrame     */    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() {            public void run() {                final NewJFrame inst = new NewJFrame();                inst.setLocationRelativeTo(null);                inst.setVisible(true);                inst.setTitle("主窗口");                inst.addWindowListener(new WindowAdapter() {                    public void windowClosing(WindowEvent e) {                        if (e.equals(inst))                        System.exit(0);                    }                });                NewJFrame inst2 = new NewJFrame();                inst2.setLocationRelativeTo(null);                inst2.setVisible(true);                inst.setTitle("子窗口");            }        });    }    public NewJFrame() {        super();        initGUI();    }    private void initGUI() {        try {            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);            pack();            setSize(400, 300);        } catch (Exception e) {            e.printStackTrace();        }    }}
------解决方案--------------------
自己看看
JFrame的
EXIT_ON_CLOSE
DISPOSE_ON_CLOSE
HIDE_ON_CLOSE
这三个有什么差别,试验一下。