Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我使用CardLayout时,如何将addWindowListener添加到所有面板?_Java - Fatal编程技术网

Java 当我使用CardLayout时,如何将addWindowListener添加到所有面板?

Java 当我使用CardLayout时,如何将addWindowListener添加到所有面板?,java,Java,我正在尝试将WindowListener添加到应用程序中的所有面板。当我关闭任何面板时,它将显示一些消息。我不知道应该把WindowListener放在哪里以及如何使用它 public MainFrame(){ this.cont= new Controller(); this.setTitle("Expense Manager"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(

我正在尝试将
WindowListener
添加到应用程序中的所有面板。当我关闭任何面板时,它将显示一些消息。我不知道应该把
WindowListener
放在哪里以及如何使用它

public MainFrame(){

    this.cont= new Controller();

    this.setTitle("Expense Manager");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);

    this.card=new CardLayout();
    this.setLayout(this.card);

    this.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            int confirmed = JOptionPane.showConfirmDialog(null, 
                "Are you sure you want to exit the program?", "Exit Program Message Box",
                JOptionPane.YES_NO_OPTION);

            if (confirmed == JOptionPane.YES_OPTION) {
              dispose();
            }
          }
        });

    this.pack();
    this.showLoginScreen();
    this.setVisible(true);


}

两件事——1。我不确定这是否是正确的方法。如果您希望在调用
card.show
时触发
WindowListener
事件,那么您会非常失望。我认为询问每个面板是否可以导航到其他面板将成为“控制器”的责任,但这是meSorry,我对java还不熟悉。我试图在用户关闭窗口后显示消息。但正如你所知,我使用cardlayout来显示不同的JPanel。然后你只有一个窗口,将它附加到it@MadProgrammer我认为主机是窗口,但它不起作用。我做了五分钟的测试,它对我工作很好,然后,我没有你所有的代码-考虑提供一个演示你的问题的可运行的例子。