可能重复:
在应用程序关闭时调用方法
我想在使用 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
}
}
}