2

我有一个程序需要保存 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 等中的一些值然后关闭程序并重新打开它时,数据没有保存。谁能解释为什么这不起作用或对我需要做的不同提出一些建议?谢谢你。

4

1 回答 1

1

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

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

于 2011-07-18T19:14:48.323 回答