退出前如何保存应用程序选项? [英] How to save application options before exit?

查看:118
本文介绍了退出前如何保存应用程序选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个应用程序,我需要在退出之前保存一些选项。(类似于窗口维度,......,将写入文件中。)

I have made an application and i need to save some options before exit.(something like window dimension, ..., that will be written in a file.)

主框架设置了这个:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

如何保存我感兴趣的选项?(在退出之前)

How can I save options that interests me?(before exiting of course)

谢谢!

推荐答案

如果您只想在应用程序关闭时执行某些操作,您可以使用以下代码挂钩关闭:

If you just want to do something when the application is shutting down, you can hook the shutdown using this code:

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        public void run() {
            // Do what you want when the application is stopping
        }
    }));

但是,这不允许您关闭窗口。如果你需要在真正退出之前检查一下,你可以覆盖 windowClosing 事件:

However, this will not allow you to not close your window. If you need to check something before really exiting, you can override the windowClosing event:

addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        // Do what you want when the window is closing.
    }
});

请注意,第一个解决方案 - 使用关闭钩子 - 的优点是它与a无关窗口事件,即使应用程序被另一个事件停止也将被执行(当然,如果Java进程被残忍地杀死)。

Note that the first solution - using the shutdown hook - has the advantage that it is not related to a window event, and will be executed even if the application is stopped by another event (except, of course, if the Java process is killed brutally).

这篇关于退出前如何保存应用程序选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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