Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 序列化在我的JFrames中不起作用_Java_User Interface_Serialization - Fatal编程技术网

Java 序列化在我的JFrames中不起作用

Java 序列化在我的JFrames中不起作用,java,user-interface,serialization,Java,User Interface,Serialization,我不知道为什么当用户正在使用时,相册没有被序列化。 在admin类中,当我登录时,userlist会在每次重新启动程序时填充到JCombobox userlist中。 但并非每次我重新启动程序时都会填充JComboBox组合框AlbumSelect。 不知道为什么。谢谢 相关代码: public class Admin extends JFrame implements ActionListener { Backend backend = new Backend(); GuiCtrl ctrl

我不知道为什么当用户正在使用时,相册没有被序列化。 在admin类中,当我登录时,
userlist
会在每次重新启动程序时填充到
JCombobox userlist
中。 但并非每次我重新启动程序时都会填充
JComboBox组合框AlbumSelect
。 不知道为什么。谢谢

相关代码:

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) {}
    }
}
}
-

公共类后端{
private HashMap userList=new HashMap();
字符串标记器;
公共后端(){
试一试{
大众主义者();
}捕获(ClassNotFoundException | IOException e){
抛出新的RuntimeException(“无法读取文件”);
}
}
公共布尔addUser(User newUser)
{
字符串userId=newUser.getID();
if(userList.containsKey(userId)){
返回false;
}
put(userId,newUser);
返回true;
}
public void storeData()引发IOException
{
尝试
{
FileOutputStream文件=新的FileOutputStream(“users.ser”);
ObjectOutputStream out=新的ObjectOutputStream(文件);
out.writeObject(用户列表);
out.close();
file.close();
}
捕获(IOI异常)
{
i、 printStackTrace();
}
}
public void populateList()引发IOException,ClassNotFoundException
{
尝试
{
FileInputStream文件=新的FileInputStream(“users.ser”);
ObjectInputStream in=新的ObjectInputStream(文件);
.readObject()中的userList=(HashMap);
in.close();
file.close();
}
捕获(IOI异常)
{
i、 printStackTrace();
userList=newhashmap();
}
}
}
  • public类相册实现可序列化{
    私有字符串名称;
    私人ArrayList照片列表;
    公共相册(字符串名称)
    {
    this.name=名称;
    photoList=newarraylist();
    }
    }
    公共类用户实现可序列化{
    私有字符串id;
    私有字符串全名;
    私有数组列表;
    公共用户(字符串id、字符串全名)
    {
    this.id=id;
    this.fullName=fullName;
    albumList=新的ArrayList();
    }
    public int addAlbum(新专辑)
    {
    相册;
    if(newAlbum==null)
    {
    返回0;
    }
    对于(int x=0;x

这看起来比严格需要的代码还要多。你能把代码进一步减少到吗?不确定需要删除什么,除非你想让我删除与管理相关的东西,因为这是可行的?理想情况下,一切;“完美是实现的,不是当没有什么可以补充的时候,而是当没有什么东西可以拿走的时候。”真的,不过,试着把它从你的一百条线下删减到百分之几下,否则真的很难去考虑它。不知道还有什么可以删除您的JFrames和JComboxes与此无关。去掉所有的Swing代码,将问题简化到最基本的部分。
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;
}
}