How can save some Objects, directly after the User has closed the Applications JFrame, but before the Program exits?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 03:27:38

问题


Good day!

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?

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

    setDefaultCloseOperation( myOwnCloseOperation );

Thanks for any help!


回答1:


  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.


来源:https://stackoverflow.com/questions/9760876/how-can-save-some-objects-directly-after-the-user-has-closed-the-applications-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!