Java 如何在退出前保存应用程序选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2361510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:33:48  来源:igfitidea点击:

How to save application options before exit?

javaswingdesktop-applicationprocessing

提问by artaxerxe

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.)

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

The main frame has set this:

主框架设置了这个:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

Thanks!

谢谢!

采纳答案by Romain Linsolas

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
        }
    }));

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

但是,这将不允许您不关闭窗口。如果您需要在真正退出之前检查某些内容,您可以覆盖该windowClosing事件:

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

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).

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

回答by PeterMmm

You may register a WindowListenerand save your options in the windowClose method.

您可以注册一个WindowListener并将您的选项保存在 windowClose 方法中。

And of course

而且当然

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

回答by ponkin

May be the following will help.

可能以下内容会有所帮助。

  1. First u need to read your property file. See doc

    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) {
      System.err.println("Ooops!");
    }
    
  2. Second add event handler to your window, which will save your data in property file.All that you need to save just put in properties instance and store it on exit

     addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
         properties.store(new FileOutputStream("filename.properties"), null);
        } catch (IOException e) {
        }
      }
    });
    
  1. 首先你需要阅读你的属性文件。见文档

    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) {
      System.err.println("Ooops!");
    }
    
  2. 其次向您的窗口添加事件处理程序,这会将您的数据保存在属性文件中。您需要保存的所有内容只需放入属性实例并在退出时存储

     addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
         properties.store(new FileOutputStream("filename.properties"), null);
        } catch (IOException e) {
        }
      }
    });
    

That's all, if I'd understood you correct)

就是这样,如果我理解你正确的话)

回答by mike

Not sure this applies to your situation, but it may to others who surf to this thread. My application has a main() and at the end has the while (!shell.isDisposed) loop. When the application runs that loop is going, and when the app is closed that loop ends and it drops to what ever is after it in the main method. This is where I put a check for if the editor is dirty and if the preferences have changed and handle those to conditions. My method attached to the file close disposes the shell and calls the closeCurrent() method but if someone clicks the 'x' close window icon it falls to here and I check again if anything is dirty and save if I need to.

不确定这是否适用于您的情况,但可能适用于浏览此线程的其他人。我的应用程序有一个 main() 并且最后有一个 while (!shell.isDisposed) 循环。当应用程序运行时,该循环将进行,当应用程序关闭时,该循环结束并下降到 main 方法中它之后的状态。这是我检查编辑器是否脏以及首选项是否已更改并根据条件处理这些内容的地方。我附加到文件 close 的方法会处理 shell 并调用 closeCurrent() 方法,但是如果有人点击“x”关闭窗口图标,它会落到这里,我会再次检查是否有任何脏东西,并在需要时保存。

while (!shlCpbtool.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    // exit
    if(dirty){
        closeCurrent();
    }