序列化无法在我的JFrame中使用

时间:2015-04-12 01:50:58

标签: java user-interface serialization

我不确定为什么用户没有将这些专辑序列化。 在admin类中,当我登录时,每次重新启动程序时userlist都会填充JCombobox<String> userlist。 但是每次重新启动程序时都不会填充JComboBox<Album> comboBoxAlbumSelect。 不知道为什么。感谢。

相关代码:

public class Admin extends JFrame implements ActionListener {
Backend backend = new Backend();
GuiCtrl ctrl = new GuiCtrl(backend);
private JComboBox<String> userlist = new JComboBox<String>();

    public Admin() {
    //same as login
    userlist = new JComboBox<String>(getUserList());
}

public String[] getUserList() {
    String[] ret = new String[backend.getUserList().size()];
    int i = 0;
    for (String s : backend.getUserList()) {
        System.out.println(s);
        ret[i++] = s;
    }
    return ret;
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == logout) {
        this.dispose();
        new Login();
    }
    else if (e.getSource() == add) {
        try {
        if (ctrl.adduser(idNumberTF.getText(), fullNameTF.getText())) {
            userlist.addItem(idNumberTF.getText());
            backend.storeData();
        } catch (IOException|ClassNotFoundException i) {}
    }
}
}
  • public class MainAlbumPanelv2 extends JFrame implements ActionListener {
    JButton btnCreateAlbum = new JButton("Create");
    JComboBox<Album> comboBoxAlbumSelect = new JComboBox<Album>();
    Backend backend = new Backend();
    GuiCtrl ctrl = new GuiCtrl(backend);
    User loggedInUser;
    
    public MainAlbumPanelv2(User id) {
        loggedInUser = id;
        comboBoxAlbumSelect = new JComboBox<Album>(getAlbumList());
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnCreateAlbum) {
            if (ctrl.createAlbum(loggedInUser, textFieldCreateAlbum.getText())) {
            Album newAlbum = new Album(textFieldCreateAlbum.getText());
            comboBoxAlbumSelect.addItem(newAlbum);
            }
            try {
            backend.storeData();
            } catch (IOException i) {}
        }
    }
    
    public Album[] getAlbumList() {
        Album[] ret = new Album[loggedInUser.getAlbums().size()];
        int i = 0;
        for (Album a : loggedInUser.getAlbums()) {
            System.out.println(a);
            ret[i++] = a;
        }
        return ret;
    }
    }
    

-

public class GuiCtrl {
User loggedInUser;
Backend backend;

    public GuiCtrl(Backend backend) {
        this.backend = backend;
    }

    public boolean adduser(String user_id, String user_name) throws IOException, ClassNotFoundException {
        if (backend.addUser(new User(user_id, user_name))) {
            return true;
        } else {
            return false;
        }
    }

    public boolean createAlbum(User user_id, String name) {
        if (user_id.getAlbum(name) != null) {
            return false;
        } else {
            user_id.addAlbum(name);
            return true;
        }
    }
}

-

public class Backend {
    private HashMap<String, User> userList = new HashMap<>();
    StringTokenizer tokenizer;

    public Backend() {
        try {
            populateList();
        } catch (ClassNotFoundException | IOException e) {
            throw new RuntimeException("Cannot read file");
        }
    }

    public boolean addUser(User newUser)
    {
        String userId = newUser.getID();
        if (userList.containsKey(userId)) {
            return false;
        }
        userList.put(userId, newUser);
        return true;
    }

    public void storeData() throws IOException
    {
        try
        {
            FileOutputStream file = new FileOutputStream("users.ser");
            ObjectOutputStream out = new ObjectOutputStream(file);
            out.writeObject(userList);
            out.close();
            file.close();
        }
        catch (IOException i)
        {
            i.printStackTrace();
        }
    }

    public void populateList() throws IOException, ClassNotFoundException
    {
        try
        {
            FileInputStream file = new FileInputStream ("users.ser");
            ObjectInputStream in = new ObjectInputStream(file);
            userList = (HashMap<String, User>) in.readObject();
            in.close();
            file.close();
        }
        catch (IOException i)
        {
            i.printStackTrace();
            userList = new HashMap();
        }
    }
}
  • public class Album implements Serializable{
    private String name;
    private ArrayList<Photo> photoList;
    
    
    public Album(String name)
    {
        this.name = name;
        photoList = new ArrayList<Photo>();
    }
    }
    
    public class User implements Serializable{
    private String id;
    private String fullName;
    private ArrayList<Album> albumList;
    public User(String id, String fullName)
    {
        this.id = id;
        this.fullName = fullName;
        albumList = new ArrayList<Album>();
    }
    public int addAlbum(Album newAlbum)
    {
        Album currAlbum;
        if (newAlbum == null)
        {
            return 0;
        }
        for (int x = 0; x < albumList.size(); x++)
        {
            currAlbum = albumList.get(x);
            if (currAlbum.getName().equals(newAlbum.getName()))
            {
                return 0;
            }
        }
        albumList.add(newAlbum);
        return 1;
    }
    public ArrayList<Album> getAlbums()
    {
        return albumList;
    }
    }
    

0 个答案:

没有答案