Java 序列化在我的JFrames中不起作用
我不知道为什么当用户正在使用时,相册没有被序列化。 在admin类中,当我登录时,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
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
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;
}
}