用java设计通讯录保存信息 设计要求:1.通讯录中包括一般通讯录的基本信息2.需要实现普通的检索功能3.通讯录写入文件4.程序执行时,从文件导入数据:退出后将输入保存到文件5.拥有可视化界面
时间: 2024-02-25 19:51:27 浏览: 114
java通讯录设计
好的,以下是一个简单的用Java设计通讯录保存信息的示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class ContactList extends JFrame implements ActionListener {
private JTextField nameField, phoneField, searchField;
private JTextArea displayArea;
private JButton addButton, searchButton, saveButton, loadButton;
private ArrayList<Contact> contacts;
public ContactList() {
super("Contact List");
contacts = new ArrayList<Contact>();
// 设置界面布局
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("Name:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("Phone:"));
phoneField = new JTextField();
inputPanel.add(phoneField);
addButton = new JButton("Add");
addButton.addActionListener(this);
inputPanel.add(addButton);
inputPanel.add(new JLabel(""));
JPanel searchPanel = new JPanel(new FlowLayout());
searchField = new JTextField(20);
searchPanel.add(searchField);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
searchPanel.add(searchButton);
JPanel displayPanel = new JPanel(new BorderLayout());
displayArea = new JTextArea(15, 30);
displayArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(displayArea);
displayPanel.add(scrollPane, BorderLayout.CENTER);
JPanel filePanel = new JPanel(new FlowLayout());
saveButton = new JButton("Save");
saveButton.addActionListener(this);
filePanel.add(saveButton);
loadButton = new JButton("Load");
loadButton.addActionListener(this);
filePanel.add(loadButton);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(searchPanel, BorderLayout.CENTER);
mainPanel.add(displayPanel, BorderLayout.SOUTH);
mainPanel.add(filePanel, BorderLayout.EAST);
add(mainPanel);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String phone = phoneField.getText();
Contact c = new Contact(name, phone);
contacts.add(c);
displayContacts();
} else if (e.getSource() == searchButton) {
String query = searchField.getText();
ArrayList<Contact> results = searchContacts(query);
displayContacts(results);
} else if (e.getSource() == saveButton) {
saveContacts();
} else if (e.getSource() == loadButton) {
loadContacts();
displayContacts();
}
}
private void displayContacts() {
displayArea.setText("");
for (Contact c : contacts) {
displayArea.append(c.getName() + " " + c.getPhone() + "\n");
}
}
private void displayContacts(ArrayList<Contact> list) {
displayArea.setText("");
for (Contact c : list) {
displayArea.append(c.getName() + " " + c.getPhone() + "\n");
}
}
private ArrayList<Contact> searchContacts(String query) {
ArrayList<Contact> results = new ArrayList<Contact>();
for (Contact c : contacts) {
if (c.getName().contains(query) || c.getPhone().contains(query)) {
results.add(c);
}
}
return results;
}
private void saveContacts() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("contacts.dat"));
out.writeObject(contacts);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadContacts() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("contacts.dat"));
contacts = (ArrayList<Contact>)in.readObject();
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ContactList();
}
}
class Contact implements Serializable {
private String name;
private String phone;
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
```
这个程序使用了Swing库实现了一个简单的GUI界面,包括输入框、按钮、文本区域等。通讯录数据以一个包含联系人姓名和电话号码的Contact类的形式保存,使用ArrayList来管理多个联系人。程序实现了添加联系人、搜索联系人、保存联系人、载入联系人等功能,并将数据以序列化的形式保存到文件中。
阅读全文