请帮我用JAVA写一份通讯录管理系统 其中包括添加、删除、修改、查看、联系人的程序 以图形实现,运用swing,Awt类
时间: 2024-05-13 20:16:26 浏览: 73
好的,下面是一份基于Java Swing和AWT类的通讯录管理系统的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ContactManager extends JFrame implements ActionListener {
private JTextField nameField, phoneField, emailField, addressField;
private JTextArea contactArea;
private JButton addButton, deleteButton, updateButton, viewButton;
public ContactManager() {
super("Contact Manager");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel formPanel = new JPanel(new GridLayout(5, 2, 10, 10));
formPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
formPanel.add(new JLabel("Name:"));
nameField = new JTextField();
formPanel.add(nameField);
formPanel.add(new JLabel("Phone:"));
phoneField = new JTextField();
formPanel.add(phoneField);
formPanel.add(new JLabel("Email:"));
emailField = new JTextField();
formPanel.add(emailField);
formPanel.add(new JLabel("Address:"));
addressField = new JTextField();
formPanel.add(addressField);
addButton = new JButton("Add");
addButton.addActionListener(this);
formPanel.add(addButton);
deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
formPanel.add(deleteButton);
updateButton = new JButton("Update");
updateButton.addActionListener(this);
formPanel.add(updateButton);
viewButton = new JButton("View All");
viewButton.addActionListener(this);
formPanel.add(viewButton);
JPanel contactPanel = new JPanel(new BorderLayout());
contactPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contactArea = new JTextArea();
contactArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(contactArea);
contactPanel.add(scrollPane, BorderLayout.CENTER);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(formPanel, BorderLayout.NORTH);
contentPane.add(contactPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String phone = phoneField.getText();
String email = emailField.getText();
String address = addressField.getText();
if (name.equals("") || phone.equals("") || email.equals("") || address.equals("")) {
JOptionPane.showMessageDialog(this, "Please fill in all fields");
} else {
String contact = name + ", " + phone + ", " + email + ", " + address;
contactArea.append(contact + "\n");
nameField.setText("");
phoneField.setText("");
emailField.setText("");
addressField.setText("");
}
} else if (e.getSource() == deleteButton) {
String selectedContact = contactArea.getSelectedText();
if (selectedContact == null) {
JOptionPane.showMessageDialog(this, "Please select a contact to delete");
} else {
String[] contactParts = selectedContact.split(", ");
String name = contactParts[0];
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete " + name + "?");
if (confirm == JOptionPane.YES_OPTION) {
String text = contactArea.getText();
contactArea.setText(text.replace(selectedContact + "\n", ""));
}
}
} else if (e.getSource() == updateButton) {
String selectedContact = contactArea.getSelectedText();
if (selectedContact == null) {
JOptionPane.showMessageDialog(this, "Please select a contact to update");
} else {
String[] contactParts = selectedContact.split(", ");
nameField.setText(contactParts[0]);
phoneField.setText(contactParts[1]);
emailField.setText(contactParts[2]);
addressField.setText(contactParts[3]);
String text = contactArea.getText();
contactArea.setText(text.replace(selectedContact + "\n", ""));
}
} else if (e.getSource() == viewButton) {
JOptionPane.showMessageDialog(this, contactArea.getText());
}
}
public static void main(String[] args) {
ContactManager contactManager = new ContactManager();
contactManager.setVisible(true);
}
}
```
这份代码实现了添加、删除、修改和查看联系人的功能。界面包括一个表单用于输入联系人的信息,以及一个文本区域用于显示已有的联系人信息。通过点击按钮可以添加、删除、修改和查看联系人信息。
阅读全文