在用户关闭Applications JFrame之后,在程序退出之前,如何直接保存一些对象? [英] How can save some Objects, directly after the User has closed the Applications JFrame, but before the Program exits?

查看:98
本文介绍了在用户关闭Applications JFrame之后,在程序退出之前,如何直接保存一些对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我正在使用NetBeans IDE开发一个小型Java应用程序,该应用程序扩展了JFrame.我实现了几种关闭应用程序的方法,例如,按 Ctrl-Q 并按JFrame的X.

I'm developping a small Java Application with NetBeans IDE that extends JFrame. I implemented several ways to close the App, for example pressing Ctrl-Q and pressing the X of the JFrame.

但是在实际关闭之前,我希望程序为我执行一些其他代码,以保存一些对象,应用程序需要在下次启动时重新使用.

But before the actual closing I'd like the program to execute some additional code for me, to save some objects the application needs to reuse the next time it starts up.

改变整个程序关闭行为的最佳方法是什么?

What is the best way to change the entire programs closing behavior?

我正在考虑覆盖/替换defaultCloseOperation,如下所示:

I'm thinking about overriding/replacing the defaultCloseOperation, something like this:

    setDefaultCloseOperation( myOwnCloseOperation );

感谢您的帮助!

推荐答案

  1. setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
  2. 添加WindowListenerWindowAdapter-覆盖windowClosing()windowClosed()方法(我忘记了哪个方法)以捕获关闭事件.
  3. 从window方法调用保存功能.
  4. 最后拨打setVisible(false)dispose()(如注释中所述).
  1. setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
  2. Add a WindowListener or WindowAdapter - overriding either the windowClosing() or windowClosed() methods (I forget which) to catch the closing event.
  3. Call the save functionality from the window method.
  4. Call setVisible(false) or dispose() (as discussed in the comments) at the end.

这篇关于在用户关闭Applications JFrame之后,在程序退出之前,如何直接保存一些对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆