java+swing通讯录管理系统
时间: 2023-09-06 09:08:15 浏览: 86
Java Swing 是一个用于 Java GUI 编程的工具包,可以帮助您创建漂亮、直观的用户界面。下面是一个简单的通讯录管理系统的示例代码:
1. 创建通讯录管理系统的 GUI 界面
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AddressBookGUI extends JFrame implements ActionListener {
// GUI 组件
private JTextField nameTextField, phoneTextField, emailTextField;
private JButton addButton, updateButton, deleteButton, clearButton;
private JList contactsList;
public AddressBookGUI() {
// 设置 GUI 界面
setTitle("通讯录管理系统");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建 GUI 组件
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("姓名:"));
nameTextField = new JTextField();
inputPanel.add(nameTextField);
inputPanel.add(new JLabel("电话:"));
phoneTextField = new JTextField();
inputPanel.add(phoneTextField);
inputPanel.add(new JLabel("邮箱:"));
emailTextField = new JTextField();
inputPanel.add(emailTextField);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
addButton = new JButton("添加");
addButton.addActionListener(this);
updateButton = new JButton("修改");
updateButton.addActionListener(this);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(clearButton);
contactsList = new JList();
JScrollPane scrollPane = new JScrollPane(contactsList);
// 将 GUI 组件添加到 JFrame 中
add(inputPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
// 实现 ActionListener 接口的 actionPerformed 方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加联系人
} else if (e.getSource() == updateButton) {
// 修改联系人
} else if (e.getSource() == deleteButton) {
// 删除联系人
} else if (e.getSource() == clearButton) {
// 清空输入框
nameTextField.setText("");
phoneTextField.setText("");
emailTextField.setText("");
}
}
public static void main(String[] args) {
new AddressBookGUI();
}
}
```
2. 创建联系人类
```
public 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;
}
// getter 和 setter 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// 重写 toString 方法
public String toString() {
return name + " " + phone + " " + email;
}
}
```
3. 在 GUI 中添加联系人
```
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 获取输入框中的信息
String name = nameTextField.getText();
String phone = phoneTextField.getText();
String email = emailTextField.getText();
// 创建 Contact 对象并添加到 JList 中
Contact contact = new Contact(name, phone, email);
DefaultListModel model = (DefaultListModel) contactsList.getModel();
model.addElement(contact);
// 清空输入框
nameTextField.setText("");
phoneTextField.setText("");
emailTextField.setText("");
}
}
```
4. 更新联系人信息
```
public void actionPerformed(ActionEvent e) {
if (e.getSource() == updateButton) {
// 获取选中的联系人
int index = contactsList.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(this, "请选择一个联系人");
return;
}
// 获取输入框中的信息并更新联系人
String name = nameTextField.getText();
String phone = phoneTextField.getText();
String email = emailTextField.getText();
Contact contact = new Contact(name, phone, email);
DefaultListModel model = (DefaultListModel) contactsList.getModel();
model.set(index, contact);
// 清空输入框
nameTextField.setText("");
phoneTextField.setText("");
emailTextField.setText("");
}
}
```
5. 删除联系人
```
public void actionPerformed(ActionEvent e) {
if (e.getSource() == deleteButton) {
// 获取选中的联系人并从 JList 中删除
int index = contactsList.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(this, "请选择一个联系人");
return;
}
DefaultListModel model = (DefaultListModel) contactsList.getModel();
model.remove(index);
// 清空输入框
nameTextField.setText("");
phoneTextField.setText("");
emailTextField.setText("");
}
}
```
这是一个简单的通讯录管理系统,您可以根据需求进行扩展。
阅读全文