将新的WindowListener添加到JFrame

时间:2012-11-03 08:43:45

标签: java swing jframe joptionpane windowlistener

    mainFrame.addWindowListener(new WindowListener() {

        @Override
        public void windowClosing(WindowEvent e) {
            if (JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to quit?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
                return;
            }
            System.exit(-1);
        }

        @Override 
        public void windowOpened(WindowEvent e) {}

        @Override 
        public void windowClosed(WindowEvent e) {}

        @Override 
        public void windowIconified(WindowEvent e) {}

        @Override 
        public void windowDeiconified(WindowEvent e) {}

        @Override 
        public void windowActivated(WindowEvent e) {}

        @Override 
        public void windowDeactivated(WindowEvent e) {}

    });

有我的代码,是否可以,因为我只使用windowClosing方法删除我的情况中的所有其他方法,无用的方法,所以它占用更少的空间?

实施例

    mainFrame.addWindowListener(new WindowListener() {

        @Override
        public void windowClosing(WindowEvent e) {
            if (JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to quit?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
                return;
            }
            System.exit(-1);
        }

    });

有可能吗?

1 个答案:

答案 0 :(得分:10)

有一个名为WindowAdapterWindowListener默认实现,它允许您覆盖您真正想要使用的方法