swing的dispose()问题

swing里窗口按钮使用dispose()来关闭窗口但是窗口的关闭监听事件 public void windowClosing(WindowEvent e) {}监听不到,点击窗口右上角的红叉就可以触发监听事件。请问怎么点击按钮关闭窗口还能触发监听事件?~

窗体没有加入监听器?
public class Demo10 {
public static void main(String[] args) {
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("hello");
System.exit(1);
}
});

f.setVisible(true);
f.setLocationRelativeTo(null);
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-05-12