【问题标题】:how to call a method before exit in jframe with java如何在使用java退出jframe之前调用方法
【发布时间】:2013-02-11 00:52:42
【问题描述】:

我试着像他们在这个问题中回答的那样 How can save some Objects, directly after the User has closed the Applications JFrame, but before the Program exits? 但框架不会关闭

我写了以下但它是同一件事,框架不会关闭。

@Override
public void windowClosed(WindowEvent e) {

    dispose();
    setVisible(false);
    System.exit(0);
}

@Override
public void windowClosing(WindowEvent e) {
    dispose();
    setVisible(false);
    System.exit(0);



}

【问题讨论】:

    标签: java swing jframe windowlistener


    【解决方案1】:

    您通常可以在 jvm 退出之前添加关闭挂钩来完成工作,不需要这些侦听器。

    Shutdown Hook

    如果你想通过监听器来做,使用 windowClosedEvent:

    @Override
    public void windowClosed(WindowEvent e) {
        save();
    }
    

    默认情况下最后一帧退出应用程序,除非您没有将默认关闭操作设置为 DISPOSE_ON_CLOSE 以外的任何值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多