【问题标题】:java gui - saving and loading datajava gui - 保存和加载数据
【发布时间】:2020-01-22 17:53:13
【问题描述】:

我有一个类库,看起来像这样:

public class Library implements Serializable{


    private static final long serialVersionUID = 1L;
    private static Library library; //a library
    private static Recommender recommender; //an instance of the recommender class
    private Set<LibraryItem> items; //set of library items
    private Set<Reader> readers; //set of readers
    private Set<Author> authors; //set of authors
    private Set<LibraryCollection> collections; //set of library collections
    private Set<Librarian> librarians; //set of the library librarians

库类是一个单例类。我构建了 gui 窗口来表示类中的不同方法,例如向库中添加阅读器。

在主类中,我有这段代码来保存和加载库:

public class MainClass implements Serializable{


    private static final long serialVersionUID = 1L;
    public static Library library;

    public static void main(String[] args)
    {
        try {
            library = loading();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        if(library == null) {
            library = Library.getInstance();
        }
}
public static Library loading() throws IOException  //loading the system object
    {
        try
        {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("librery.srl"));
            library = (Library)in.readObject();
            JOptionPane.showMessageDialog(null,"Loading librery.ser file to the database", "Loading File", JOptionPane.INFORMATION_MESSAGE);
            return library;
        }

        //in case the file is not found
        catch (FileNotFoundException e)
        {
            JOptionPane.showMessageDialog(null,"File wasn't found, creating new library:", "Missing file", JOptionPane.ERROR_MESSAGE);
            return library;

        }

        //in case there was a problem loading data from file
        catch(IOException eio)
        {
            JOptionPane.showMessageDialog(null,"system wasn't able to read from file.. creating new library", "Read File Error", JOptionPane.INFORMATION_MESSAGE);
            return library;
        }

        //genreal exceptions
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return library;
        }
    }//end of loading method

    public static void save(Library library) throws IOException
    {
        //trying to save the data
        try
        { 
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("librery.srl"));
            out.writeObject(library);
            out.close();
        }

        //general exceptions
        catch (Exception e)
        { 
            JOptionPane.showMessageDialog(null,"Saving file was failed", "Save Error", JOptionPane.ERROR_MESSAGE);
        }
    }//end of save method

}

我以这种方式保存库:

public class SaveLibrary extends JFrame {

    private static final long serialVersionUID = 1L;

    public SaveLibrary() {
        super("Save Library");
JLabel lblDoYouWant = new JLabel("Do you want to save the library?");
        lblDoYouWant.setIcon(new ImageIcon("icons/icon.png"));
        lblDoYouWant.setBounds(0, 0, 434, 22);
        lblDoYouWant.setVerticalAlignment(SwingConstants.BOTTOM);
        lblDoYouWant.setHorizontalAlignment(SwingConstants.LEFT);
        lblDoYouWant.setFont(new Font("Tahoma", Font.BOLD, 18));
        lblDoYouWant.setBackground(new Color(216, 191, 216));
        getContentPane().add(lblDoYouWant);

        JRadioButton rdbtnYes = new JRadioButton("Yes");
        rdbtnYes.setIcon(new ImageIcon("icons/yes.png"));
        rdbtnYes.setHorizontalAlignment(SwingConstants.LEFT);
        rdbtnYes.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                //if the button is selected save the library and exit the program
                try {
                    MainClass.save(MainClass.library);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.exit(1);
            }
        });
        rdbtnYes.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
        rdbtnYes.setBackground(new Color(216, 191, 216));
        rdbtnYes.setBounds(0, 48, 109, 23);
        getContentPane().add(rdbtnYes);
}

当我运行程序时,它不会将数据从一次运行保存到另一次。我的保存和加载方法有什么需要更改的吗? 我只是不明白为什么它不起作用。

【问题讨论】:

    标签: java user-interface save serializable


    【解决方案1】:

    如果您想序列化库,那么 Recommender、LibraryItem,... 也必须是可序列化的。 为了控制事物的序列化方式,请考虑使用 Externalizable 而不是 Serializable。

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      相关资源
      最近更新 更多