java - 将新的 WindowListener 添加到 JFrame

标签 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);
        }

    });

这可能吗?

最佳答案

WindowListener 的默认实现称为 WindowAdapter这允许您覆盖您真正想要使用的方法

关于java - 将新的 WindowListener 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13207519/

相关文章:

如果我添加 JButton,Java FocusListener 和 KeyListener 将不起作用

java - 将 JFrame 中的现有 JPanel 替换为另一个 JPanel 的正确方法是什么?

java - 如何设置JFrame图标图像?

java - JFrame 实例无法可视化添加的组件

java - Spring RestTemplate拦截器不执行请求

java - 如何在intelliJ中运行Java项目到服务器

java - 无法刷新 JFrame

java - 使用 Swing 显示 GUI 框架

java - 如何让我的 JRE 真正在 32 位模式下运行?

java - 如何并行输出行号?