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/Savingandrestoringthestateofclasses.htm

すべてのオブジェクトがシリアル化できるわけではありませんが、最初のリンクで述べられているように、「... Swing GUIコンポーネント、文字列、および配列はシリアル化可能です」。シリアル化可能なインターフェイスを実装する独自のクラスを作成できます。

于 2011-07-18T19:14:48.323 に答える