2

可能重复:
在应用程序关闭时调用方法

我想在使用 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 关闭我的 java 应用程序之前调用一个方法;我已经用 windowClosed/windowsClosing 尝试了 WindowListener,但它不起作用。它看起来像这样

public class exampleFrame JFrame implements{

public exampleFrame (){
    this.addWindowListener(new windowAction());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class windowAction implements WindowListener{

        @Override
        public void windowActivated(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowClosed(WindowEvent e) {
        System.out.prinln("window closed")

        }

        @Override
        public void windowClosing(WindowEvent e) {
            // TODO Auto-generated method stub
            System.out.prinln("window closing")
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowIconified(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowOpened(WindowEvent e) {
            // TODO Auto-generated method stub

        }

    }

}
4

1 回答 1

-1

我认为你想要的是这样的:

public class ExampleFrame extends JFrame{
   public ExampleFrame(){

      // you can use WindowAdapter and implement only the methods you need
      addWindowListner(new WindowAdapter(){

           @Override
           public void windowClosing(WindowEvent et) {
               System.out.println("Window closing");
           }
      }
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
于 2012-12-12T00:46:19.883 回答