java中数组反序列化_Java GUI中的数组反序列化问题

现在我需要一些帮助序列化我的arraylist。现在我设法使序列化方面起作用(至少我认为),现在我的问题是解序列化对象。我正在制作一个小地址簿程序。我有一个comboBox存储用户输入的三个文本框的地址:名称,地址和电话号码。出于测试目的,我有一个保存和加载按钮。保存按钮保存联系人,并且加载按钮加载以前的会话联系人。现在,除了反序列化工作以外,所有的东西我都想知道如何继续下去。

我的代码如下:

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 test = new ArrayList();

ArrayList array = new ArrayList();

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

final JComboBox comboBox = new JComboBox();

final DefaultComboBoxModel model = new DefaultComboBoxModel();

/**

* 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) 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);

}

}这是我的目标:

public 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);

}

}

}代码的下一个斑点在我的Address_Book类中,这是我给你的第一个代码,这只是为了更容易找到我从哪里加载它。

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) 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);谢谢你的时间,如果你有任何问题,请不要犹豫。 :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值