【问题标题】:Need help using SingleFrameApplication to save sessions需要帮助使用 SingleFrameApplication 保存会话
【发布时间】:2011-07-23 02:35:05
【问题描述】:

我有一个程序需要保存 JTextFields、JComboBoxes 等中的所有内容。

我遇到了一个例子,让我相信我可以通过 SingleFrameApplication 类实现这一点。

如果序列化,该程序中有 1000 多个组件需要跟踪。

这是我目前所拥有的:

public class NewMain extends SingleFrameApplication{

    //the file for the session to be saved to
    String sessionFile = "sessionState.xml";
    //Container is a class I made that extends a JPanel
    Container container;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    Application.launch(NewMain.class, args);
    }

    @Override
    protected void startup() {
        try {
            //load the file
            getContext().getSessionStorage().restore(getMainFrame(), sessionFile);
            container = new Container(getContext());
            show(container);

        }
        catch (Exception e) {

        }
    }

    @Override
    protected void shutdown() {
        try {
            //save to the file so we can open it later
            getContext().getSessionStorage().save(getMainFrame(), sessionFile);
        }
        catch (Exception e) {

        }
    }
}

当我打开运行 .jar 文件并更改 JTextFields、JComboBoxes 等中的一些值然后关闭程序并重新打开它时,数据没有保存。谁能解释为什么这不起作用或对我需要做的不同提出一些建议?谢谢。

【问题讨论】:

    标签: java swing-app-framework session-storage


    【解决方案1】:

    我建议您在这种情况下使用序列化。 看看这个: http://java.sun.com/developer/technicalArticles/Programming/serialization/ 或者: http://www.java2s.com/Tutorial/Java/0180__File/Savingandrestoringthestateofclasses.htm

    并非所有对象都是可序列化的,但正如第一个链接中所说,“...Swing GUI 组件、字符串和数组 -- 是可序列化的”,您可以编写自己的类来实现可序列化接口。

    【讨论】:

    • 我真的不明白这对我有什么帮助,你能解释一下什么是序列化以及在这种情况下我将如何去做吗?
    • 再次,从第一个链接...“对象序列化是将对象的状态保存为字节序列的过程,以及在将来将这些字节重建为活动对象的过程时间。”您想序列化容器中的所有内容,据我了解,这些是摆动组件,因此您需要做的是将它们序列化为一个文件,并在您再次需要它们时反序列化它们。
    • 在关机时序列化所有你想要的东西,在初始化或启动时反序列化
    • 序列化所有组件的问题是容器 JPanel 包含一个 JTabbedPane,它包含另外 10 个 JPanel。这个程序有1000+个组件,不是一个小程序,全部序列化是不是效率很低?
    • 如果你只对你需要的组件进行序列化-反序列化,并且尽可能少地做,效率将会提高。就性能而言,它也可能存在问题,因此您需要重新考虑这一点,并可能运行一些测试以查看它是否符合您的需求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2011-07-15
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多