javagui中的数组反序列化问题

javagui中的数组反序列化问题,java,user-interface,serialization,arraylist,deserialization,Java,User Interface,Serialization,Arraylist,Deserialization,现在我需要一些帮助来序列化我的arraylist。现在我已经设法让序列化方面工作了(至少我认为是这样),现在我的问题在于对对象进行反序列化。我正在制作一个小的通讯录程序。我有一个组合框,用于存储地址,上面有三个文本框供用户输入:姓名、地址和电话号码。出于测试目的,我有一个保存和加载按钮。“保存”按钮保存联系人,“加载”按钮加载以前的会话联系人。现在,除了反序列化之外,其他一切都正常工作了,我想知道如何继续进行 我的代码如下: import java.awt.EventQueue; import

现在我需要一些帮助来序列化我的arraylist。现在我已经设法让序列化方面工作了(至少我认为是这样),现在我的问题在于对对象进行反序列化。我正在制作一个小的通讯录程序。我有一个组合框,用于存储地址,上面有三个文本框供用户输入:姓名、地址和电话号码。出于测试目的,我有一个保存和加载按钮。“保存”按钮保存联系人,“加载”按钮加载以前的会话联系人。现在,除了反序列化之外,其他一切都正常工作了,我想知道如何继续进行

我的代码如下:

import java.awt.EventQueue;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class Address_Book {

    private JFrame frame;
    private JTextField newName;
    private JTextField newAddress;
    private JTextField newPhoneAddress;
    ArrayList<Book> test = new ArrayList<Book>();
    ArrayList<Book> array = new ArrayList<Book>();

    File addBook = new File("addBook.txt");

    final JComboBox<String> comboBox = new JComboBox<String>();
    final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Address_Book window = new Address_Book();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Address_Book() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {         
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        newName = new JTextField();
        newName.setBounds(10, 29, 107, 20);
        frame.getContentPane().add(newName);
        newName.setColumns(10);

        JLabel lbl1 = new JLabel("Enter New Name:");
        lbl1.setBounds(10, 11, 107, 14);
        frame.getContentPane().add(lbl1);

        JLabel lbl2 = new JLabel("Enter New Address:");
        lbl2.setBounds(136, 11, 130, 14);
        frame.getContentPane().add(lbl2);

        newAddress = new JTextField();
        newAddress.setColumns(10);
        newAddress.setBounds(136, 29, 107, 20);
        frame.getContentPane().add(newAddress);

        newPhoneAddress = new JTextField();
        newPhoneAddress.setColumns(10);
        newPhoneAddress.setBounds(262, 29, 162, 20);
        frame.getContentPane().add(newPhoneAddress);

        JLabel lbl3 = new JLabel("Enter New Phone number:");
        lbl3.setBounds(262, 11, 162, 14);
        frame.getContentPane().add(lbl3);

        JButton btnAddNewContact = new JButton("Add new contact");
        btnAddNewContact.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent arg0) {
                test.add((new Book(newName.getText(), newAddress.getText(), newPhoneAddress.getText())));

                //mergesort.mergesort(test, 0, test.size() - 1);

                model.removeAllElements();
                for(int i=0; i < test.size();i++){
                    model.addElement(test.get(i).getContact()); 
                }
                comboBox.setModel(model);

                newName.setText(""); 
                newAddress.setText("");
                newPhoneAddress.setText("");
            }
        });
        btnAddNewContact.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnAddNewContact.setBounds(10, 53, 414, 23);
        frame.getContentPane().add(btnAddNewContact);

        JLabel lbl4 = new JLabel("Current Contacts:");
        lbl4.setBounds(10, 87, 107, 14);
        frame.getContentPane().add(lbl4);

        frame.getContentPane().add(comboBox);


            comboBox.setModel(model);
            comboBox.setBounds(10, 101, 414, 20);
            comboBox.setSelectedIndex(test.size()-1);

            JButton btnLoad = new JButton("Load");
            btnLoad.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {

                    try {
                        /* Read objects */

                        FileInputStream in = new FileInputStream(addBook);
                        ObjectInputStream readIn = new ObjectInputStream(in);

                        array = (ArrayList<Book>) readIn.readObject();
                        readIn.close(); 

                        for(int i=0; i < array.size();i++){
                            model.addElement(array.get(i).getContact());    
                        }
                        comboBox.setModel(model);
                    }catch(Exception e1){
                        e1.printStackTrace();
                    }

                }
            });
            btnLoad.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                }
            });
            btnLoad.setBounds(10, 132, 89, 23);
            frame.getContentPane().add(btnLoad);

            JButton btnSave = new JButton("Save");
            btnSave.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent arg0) {
                    /* write objects */
                    try{

                        FileOutputStream out = new FileOutputStream(addBook);
                        ObjectOutputStream writeAdd = new ObjectOutputStream(out);
                        writeAdd.writeObject(test);
                        writeAdd.close();

                    }catch(Exception e){

                    }
                }
            });
            btnSave.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
            btnSave.setBounds(109, 132, 89, 23);
            frame.getContentPane().add(btnSave);
    }
}
导入java.awt.EventQueue;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JTextField;
导入javax.swing.DefaultComboxModel;
导入javax.swing.JLabel;
导入javax.swing.JButton;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.util.ArrayList;
公共类通讯录{
私有JFrame;
私有JTextField newName;
私有JTextField newAddress;
私有JTextField newPhoneAddress;
ArrayList测试=新的ArrayList();
ArrayList数组=新的ArrayList();
文件addBook=新文件(“addBook.txt”);
最终JComboBox组合框=新JComboBox();
最终DefaultComboxModel=新的DefaultComboxModel();
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
地址簿窗口=新地址簿();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共广播书{
初始化();
}
/**
*初始化框架的内容。
*/
私有void initialize(){
frame=新的JFrame();
机架立根(100450250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
newName=newjtextfield();
newName.setBounds(10,29,107,20);
frame.getContentPane().add(newName);
newName.setColumns(10);
JLabel lbl1=新JLabel(“输入新名称:”);
lbl1.立根(10,11,107,14);
frame.getContentPane().add(lbl1);
JLabel lbl2=新JLabel(“输入新地址:”);
lbl2.挫折(136,11,130,14);
frame.getContentPane().add(lbl2);
newAddress=newjtextfield();
newAddress.setColumns(10);
新地址.立法院(136,29,107,20);
frame.getContentPane().add(newAddress);
newPhoneAddress=new JTextField();
newPhoneAddress.setColumns(10);
newPhoneAddress.setBounds(2622916220);
frame.getContentPane().add(newPhoneAddress);
JLabel lbl3=新JLabel(“输入新电话号码:”);
lbl3.立根(262,11,162,14);
frame.getContentPane().add(lbl3);
JButton btnAddNewContact=新JButton(“添加新联系人”);
btnAddNewContact.addMouseListener(新的MouseAdapter(){
@凌驾
public void mousePressed(MouseEvent arg0){
test.add((新书(newName.getText(),newAddress.getText(),newPhoneAddress.getText()));
//mergesort.mergesort(test,0,test.size()-1);
model.removeAllElements();
对于(int i=0;ipublic class Book implements Comparable {
     private String flName, Address, pNumber;

    public Book(String Name, String address, String phoneNumber ){
        setFlName(Name);
        setAddress(address);
        setpNumber(phoneNumber);
    }

    public String getpNumber() {
        return pNumber;
    }

    public void setpNumber(String pNumber) {
        this.pNumber = pNumber;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public String getFlName() {
        return flName;
    }

    public void setFlName(String flName) {
        this.flName = flName;
    }  

    public String getContact() {
        return flName + ", " + Address + ", " + pNumber;
    }

    public int compareTo(Object c) {
        Book testBook = (Book)c;

        if (testBook.getFlName().compareTo(this.getFlName()) < 0){
            return(-1);
        }else if(testBook.getFlName().compareTo(this.getFlName()) == 0){
            return(0);
        }else{
            return(1);
        }
    }

}
JButton btnLoad = new JButton("Load");
            btnLoad.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {

                    try {
                        /* Read objects */

                        FileInputStream in = new FileInputStream(addBook);
                        ObjectInputStream readIn = new ObjectInputStream(in);

                        array = (ArrayList<Book>) readIn.readObject();
                        readIn.close(); 

                        for(int i=0; i < array.size();i++){
                            model.addElement(array.get(i).getContact());    
                        }
                        comboBox.setModel(model);
                    }catch(Exception e1){
                        e1.printStackTrace();
                    }

                }
            });
            btnLoad.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                }
            });
            btnLoad.setBounds(10, 132, 89, 23);
            frame.getContentPane().add(btnLoad);