java - 关闭 JDialog 时没有 WindowEvent

标签 java swing jdialog windowlistener

我在 JFrame 中显示 JDialog。此 JDialog 对处理不执行任何操作。我想捕捉关闭事件并显示一个弹出窗口,但没有任何反应。

我找不到错误。你能告诉我问题出在哪里吗?

非常感谢!

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;


@SuppressWarnings("serial")
public class JFrameTest extends JFrame {

    public JFrameTest() {
        setLayout(new FlowLayout());
        setSize(300, 300);
        add(new JTextArea("This is a text"));
        setDefaultCloseOperation(JFrameTest.EXIT_ON_CLOSE);
        getContentPane().setPreferredSize(getSize());
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        JDialogTest dialog = new JDialogTest(this, Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        new JFrameTest();
    }

    private class JDialogTest extends JDialog implements WindowListener {

        public JDialogTest(Window parent, ModalityType modalityType) {
            super(parent, modalityType);

            setLayout(new FlowLayout());
            add(new JLabel("This is another text"));
            setSize(200, 50);

            setDefaultCloseOperation(JDialogTest.DO_NOTHING_ON_CLOSE);
            setLocationRelativeTo(null);
            getContentPane().setPreferredSize(getSize());
            pack();
            setVisible(true);
        }

        @Override
        public void windowActivated(WindowEvent e) {}

        @Override
        public void windowClosed(WindowEvent e) {}

        @Override
        public void windowClosing(WindowEvent e) {
            JOptionPane.showMessageDialog(this, "A popup message!");
        }

        @Override
        public void windowDeactivated(WindowEvent e) {}

        @Override
        public void windowDeiconified(WindowEvent e) {}

        @Override
        public void windowIconified(WindowEvent e) {}

        @Override
        public void windowOpened(WindowEvent e) {}
    }
}

最佳答案

您忘记将 WindowListener 添加到 JDialogTest 类,以捕获 WINDOW CLOSING 事件。像这样:

addWindowListener(this);

此外,您在 JDialogTest 类中调用一次 setVisible(true),另一次在 JFrameTest 类中创建 JDialogTest 类的对象时调用。

请永远不要在 Swing 中使用任何 setXXXSize(...) 方法,让布局管理器来处理那部分。此外,明智的做法是使用 setLocationByPlatform(true) 而不是 setLocationRelativeTo(null)。 @Andrew Thompson 在 How to best position Swing GUI's 的线程中给出了一个很好的例子,说明为什么应该使用前者而不是后者。

关于java - 关闭 JDialog 时没有 WindowEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519522/

相关文章:

java - Log4j2 自定义附加程序 : ERROR Attempted to append to non-started appender

java - Java 中 "while"的问题

java - Swing 中的计时器任务

java - 立即更新到 Java 中的 JCombobox

java - 非常奇怪的 Java2D setClip() 效果 - 错误?

java - 抛出 Swing 异常时显示对话框

java - 为什么 Joptionpane 在 JDialog 之上不是模态的

java - 我如何解析带有德国变音符号的 XML!名字?

java - 运行 Spring 集成测试时为 "org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same"

java - 切换浏览器选项卡时隐藏小程序的 JDialog