简体   繁体   English

JFrame关闭操作

[英]JFrame close operation

How can I set another close operation for my JFrame besides the myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ? 除了myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)之外,如何为我的JFrame设置另一个关闭操作? Example: I got a start() and a stop() method and I want the stop() to be called before the JFrame exits ( stop() saves my files and joins the Thread). 示例:我有一个start()和stop()方法,我希望在JFrame退出之前调用stop()(stop()保存我的文件并加入Thread)。

Add a WindowListener to your JFrame and implement the windowClosing() event: WindowListener添加到JFrame并实现windowClosing()事件:

// Use WindowAdapter if you don't want to implement the rest of the methods
// otherwise use new WindowListener()
window.addWindowListener( new WindowAdapter() { 
    @Override
    public void windowClosing(WindowEvent e) {
        stop();
    }});            

Override the dispose method. 覆盖dispose方法。

@Override
public void dispose() {
  this.stop();
  super.dispose();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM