【问题标题】:How to save the last selected folder after closing application?关闭应用程序后如何保存最后选择的文件夹?
【发布时间】:2015-09-03 12:11:23
【问题描述】:

你能告诉我,如何在 java swing 应用程序中保存最后选择的文件夹,以便下次激活我的应用程序时它会自动加载?

我正在编写一个读取图像并形成幻灯片的应用程序。除了记录所选文件夹外,我什么都有?

有什么想法吗?

【问题讨论】:

标签: java swing serialization awt application-settings


【解决方案1】:

有什么想法吗?

有很多可能的方法,但这里有几个直截了当的方法:

  • 记录您希望在Properties 对象中保留的应用程序状态。当应用程序退出时,使用其中一种保存/存储方法将属性保存到文件中。当应用程序重新启动时,从文件中加载状态...如果存在。

  • 阅读Java Preferences API。

【讨论】:

    【解决方案2】:

    我认为对你来说最简单的方法是 Preferences 类,基本上是这样工作的:

    //You can get the stored path and for the case there isn´t any you set the default path
    Preferences lastdir = Preferences.systemNodeForPackage(Main.class);
    String dirStr = lastdir.get("lastdir","c:/default");
    
    //Then you store the new value each time you change the path
    lastdir.put("lastdir","c:/new dir");//Stores the new value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多