Java通过图形页面实现通讯录管理系统代码
时间: 2024-05-02 11:16:56 浏览: 90
以下是一个简单的 Java 图形页面通讯录管理系统的代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class AddressBookGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField nameField, phoneField, emailField;
private JTextArea outputArea;
private JButton addButton, findButton, listButton;
private ArrayList<Contact> contacts = new ArrayList<Contact>();
public AddressBookGUI() {
super("Address Book");
setLayout(new BorderLayout());
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);
inputPanel.add(new JLabel("Email:"));
emailField = new JTextField("");
inputPanel.add(emailField);
add(inputPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
addButton = new JButton("Add");
addButton.addActionListener(this);
buttonPanel.add(addButton);
findButton = new JButton("Find");
findButton.addActionListener(this);
buttonPanel.add(findButton);
listButton = new JButton("List");
listButton.addActionListener(this);
buttonPanel.add(listButton);
add(buttonPanel, BorderLayout.CENTER);
outputArea = new JTextArea();
add(new JScrollPane(outputArea), BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == addButton) {
String name = nameField.getText();
String phone = phoneField.getText();
String email = emailField.getText();
Contact contact = new Contact(name, phone, email);
contacts.add(contact);
outputArea.append("Contact added: " + contact + "\n");
nameField.setText("");
phoneField.setText("");
emailField.setText("");
}
else if (event.getSource() == findButton) {
String name = nameField.getText();
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
outputArea.append(contact + "\n");
return;
}
}
outputArea.append("Contact not found.\n");
}
else if (event.getSource() == listButton) {
outputArea.setText("");
for (Contact contact : contacts) {
outputArea.append(contact + "\n");
}
}
}
public static void main(String[] args) {
AddressBookGUI addressBook = new AddressBookGUI();
}
}
class Contact {
private String name;
private String phone;
private String email;
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
public String toString() {
return name + " " + phone + " " + email;
}
}
```
这个程序创建了一个简单的图形用户界面,包含了输入联系人信息的文本框和添加、查找和列出联系人的按钮。联系人信息保存在一个 Contact 类的对象中,这个类包含了名字、电话号码和电子邮件地址。添加联系人时,程序将创建一个新的 Contact 对象,并将其添加到 contacts 列表中。查找联系人时,程序将在 contacts 列表中查找具有相同名字的 Contact 对象。列出所有联系人时,程序将遍历整个 contacts 列表,并将每个 Contact 对象的字符串表示添加到输出区域中。
阅读全文