Java读写ArrayList类型与GUI联系

时间:2013-05-15 02:54:32

标签: java user-interface file-io arraylist

修改

我做了几次修正,现在可以编译了。尝试向文件添加联系人时,我在ContactsCollection类的第13行收到空指针异常:

for (int i = 0; i < contactList.size(); i++)
联系人(GUI)类的

和第93行:

contactList.add(contact);

联系人课程:

import java.util.*;
import java.io.*;

public class Contact implements Serializable
{
public static final long serialVersionUID = 42L;
public String name, number;

Contact()
{
    name = "No name";
    number = "No number";
}

Contact (String theName, String theNumber)
{
    this.name = theName;
    this.number = theNumber;
}

public void setName(String aName)
{
    this.name = aName;
}

public void setNumber(String aNumber)
{
    this.number =aNumber;
}

public String getName()
{
    return name;
}

public String getNumber()
{
    return number;
}

public String toString()
{
    return name + ": " + number;
}

public boolean equals(Contact other)
{
   if (name.equals(other.getName()) && number.equals(other.getNumber()))
   {
      return(true);
   }
   else
   {
      return(false);
   }
}   
}

联系人集合类

import java.io.*;
import java.util.*;

class ContactsCollection
{
public static ArrayList<Contact> contactList;

public static void write()
{
    try 
    {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("contactList.dat"));
        for (int i = 0; i < contactList.size(); i++)
        {
            out.writeObject(contactList.get(i));
        }
        out.close();
    } 
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

public static void read()
{
contactList = new ArrayList<Contact>();
try
{
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("contactList.dat"));
    Contact temp;
    while (in.available()!=0)
    {
        temp = (Contact)in.readObject();
        contactList.add(temp);
    }
    in.close();
}
catch(FileNotFoundException e)
{
    e.printStackTrace();
}
catch(IOException e)
{
    e.printStackTrace();
}
catch(ClassNotFoundException e)
{
    e.printStackTrace();
}       

}
}

联系人(GUI)类

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.util.*;

class Contacts extends JFrame implements ActionListener, WindowListener
{
public static final int WIDTH = 400;
    public static final int HEIGHT = 600;
    public static final int SMALL_WIDTH = 200;
    public static final int SMALL_HEIGHT = 100;

private static final Dimension stdBtn = new Dimension(150, 50);    

    JPanel centerPanel, northPanel, southPanel;
ImageIcon icon;
JLabel picture;
JButton addContact, displayContacts;
JScrollPane scroll;
JTextArea textArea;
Clock clock;
Background background;
Contact contact;
ArrayList<Contact> contactList;


public Contacts()
{
    super("Contacts");
    this.setLayout(new BorderLayout());
    this.setSize(WIDTH, HEIGHT);
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    addWindowListener(this);
    this.setLocationRelativeTo(null);

    centerPanel = new JPanel();
    northPanel = new JPanel();
    southPanel = new JPanel();

    centerPanel.setBackground(Color.BLACK);
    southPanel.setBackground(Color.BLACK);      

    clock = new Clock();
    northPanel.add(clock);

    icon = new ImageIcon("ContactsBackground.jpg");
    picture = new JLabel(icon);
    centerPanel.add(picture);

    textArea = new JTextArea("", 10, 30);
    textArea.setEditable(false);
    JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    
    centerPanel.add(scroll);    

    JButton displayContacts = new JButton("Display contacts");
    displayContacts.addActionListener(this);
    southPanel.add(displayContacts);

    JButton addContact = new JButton("Add contact");
    addContact.addActionListener(this);
    southPanel.add(addContact);

    this.add(northPanel, BorderLayout.NORTH);
    this.add(centerPanel, BorderLayout.CENTER);
    this.add(southPanel, BorderLayout.SOUTH);       

    setResizable(false);        
}

public void actionPerformed(ActionEvent e)
{
    contactList = new ArrayList<Contact>();
    JButton source = (JButton)e.getSource();
    String contactInput = source.getText();

    if (contactInput == "Display contacts")
    {
        ContactsCollection.read();
        for (int i = 0; i < contactList.size(); i++)
        {
            contact = (Contact)contactList.get(i);
            textArea.setText(contact.getName() + "," + contact.getNumber() + "\n");
        }
    }
    if (contactInput == "Add contact")
    {
        String name = JOptionPane.showInputDialog(null, "Enter Name");
        String number = JOptionPane.showInputDialog(null, "Enter Number");
        contact = new Contact(name, number);
        contactList.add(contact);
        ContactsCollection.write();
    }
}

public void windowOpened(WindowEvent e)
{}

public void windowClosing(WindowEvent e)
{
    this.setVisible(false);
    background = new Background();
    background.setVisible(true);        
}

public void windowClosed(WindowEvent e)
{}

public void windowIconified(WindowEvent e)
{}

public void windowDeiconified(WindowEvent e)
{}

public void windowActivated(WindowEvent e)
{}

public void windowDeactivated(WindowEvent e)
{}      
}

1 个答案:

答案 0 :(得分:0)

关于

  

我遇到的第一个错误是“找不到符号方法read()”和“找不到符号方法write()”。

你正在调用一个不存在的ArrayList read()上的方法。您需要在ArrayList的内容之一上调用此方法,而不是ArrayList本身。这就像试图用蛋箱做煎蛋 - 不会起作用。你需要用一两个鸡蛋代替。 write()也是如此。

关于

  

第二个错误发生在显示联系人的动作侦听器中,表示“需要一个数组但找不到联系人”。

您应该告诉我们哪一行会导致此错误。


修改
你表明:

for (int i = 0; i < contactList.size(); i++)
{
    textArea.setText(contact[i].getName() + "," + contact [i].getNumber() + "\n");
}

那段代码毫无意义。您需要使用列表变量contactList并通过ArrayList get(...)方法获取其项目。


您的代码表明,无论出现何种错误,您都会盲目添加一堆代码,但这样做无效。如果您不能使用IDE,则应在每1-3行后经常编译并修复所有编译错误,然后再尝试添加更多代码。否则你最终会遇到老鼠的错误。另外,请阅读手册/书籍。不要猜这个东西。