使用Java程序完成以下要求的图形用户界面并详细注释代码。 编写一个个人通信录程序,具体如下的功能: 定义一个类,包含姓名、邮政编码、通信地址、电话、手机、email等成员变量。 添加:向文件中写个人信息。 查找:根据姓名在文件中查找个人信息,如果找到则显示出来,有提示信息显示操作是否成功。 清空:清空所有文本框中的信息 退出:退出当前页面 删除:根据姓名在文件中查找个人信息并删除,有提示信息显示操作是否成功。 所有的文本框都在同一列上 所有按钮都位于文本框正下方同一行 显示的提示信息位于最下方
时间: 2023-12-06 13:40:12 浏览: 74
以下是注释详细的Java程序代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PersonalAddressBook extends JFrame implements ActionListener {
// 定义GUI组件,用于用户界面
private JLabel nameLabel, postCodeLabel, addressLabel, phoneLabel, mobileLabel, emailLabel;
private JTextField nameField, postCodeField, addressField, phoneField, mobileField, emailField;
private JButton addButton, searchButton, deleteButton, clearButton, exitButton;
// 定义一个内部类,用于存储个人信息
private class Person {
String name, postCode, address, phone, mobile, email;
public Person(String name, String postCode, String address, String phone, String mobile, String email) {
this.name = name;
this.postCode = postCode;
this.address = address;
this.phone = phone;
this.mobile = mobile;
this.email = email;
}
@Override
public String toString() {
return name + "," + postCode + "," + address + "," + phone + "," + mobile + "," + email;
}
}
public PersonalAddressBook() {
super("个人通信录");
// 初始化GUI组件
nameLabel = new JLabel("姓名:");
postCodeLabel = new JLabel("邮政编码:");
addressLabel = new JLabel("通信地址:");
phoneLabel = new JLabel("电话:");
mobileLabel = new JLabel("手机:");
emailLabel = new JLabel("Email:");
nameField = new JTextField(20);
postCodeField = new JTextField(20);
addressField = new JTextField(20);
phoneField = new JTextField(20);
mobileField = new JTextField(20);
emailField = new JTextField(20);
addButton = new JButton("添加");
searchButton = new JButton("查找");
deleteButton = new JButton("删除");
clearButton = new JButton("清空");
exitButton = new JButton("退出");
// 设置GUI界面的布局
JPanel inputPanel = new JPanel(new GridLayout(6, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(postCodeLabel);
inputPanel.add(postCodeField);
inputPanel.add(addressLabel);
inputPanel.add(addressField);
inputPanel.add(phoneLabel);
inputPanel.add(phoneField);
inputPanel.add(mobileLabel);
inputPanel.add(mobileField);
inputPanel.add(emailLabel);
inputPanel.add(emailField);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
buttonPanel.add(deleteButton);
buttonPanel.add(clearButton);
buttonPanel.add(exitButton);
JPanel messagePanel = new JPanel();
messagePanel.add(new JLabel("提示信息:"));
// 将GUI组件添加到窗口中
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(inputPanel, BorderLayout.CENTER);
container.add(buttonPanel, BorderLayout.SOUTH);
container.add(messagePanel, BorderLayout.NORTH);
// 添加事件监听器
addButton.addActionListener(this);
searchButton.addActionListener(this);
deleteButton.addActionListener(this);
clearButton.addActionListener(this);
exitButton.addActionListener(this);
// 设置窗口的大小和位置,并显示窗口
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == addButton) {
// 添加个人信息
Person person = new Person(nameField.getText(), postCodeField.getText(), addressField.getText(),
phoneField.getText(), mobileField.getText(), emailField.getText());
writeToFile(person.toString());
showMessage("添加成功");
} else if (source == searchButton) {
// 查找个人信息
String name = nameField.getText();
String result = searchByName(name);
if (result != null) {
String[] fields = result.split(",");
nameField.setText(fields[0]);
postCodeField.setText(fields[1]);
addressField.setText(fields[2]);
phoneField.setText(fields[3]);
mobileField.setText(fields[4]);
emailField.setText(fields[5]);
showMessage("查找成功");
} else {
showMessage("查找失败,没有找到指定的姓名");
}
} else if (source == deleteButton) {
// 删除个人信息
String name = nameField.getText();
if (deleteByName(name)) {
clearFields();
showMessage("删除成功");
} else {
showMessage("删除失败,没有找到指定的姓名");
}
} else if (source == clearButton) {
// 清空所有文本框中的信息
clearFields();
} else if (source == exitButton) {
// 退出程序
System.exit(0);
}
}
// 将个人信息写入文件
private void writeToFile(String str) {
try {
FileWriter writer = new FileWriter("addressbook.txt", true);
writer.write(str + "\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 根据姓名查找个人信息
private String searchByName(String name) {
try {
BufferedReader reader = new BufferedReader(new FileReader("addressbook.txt"));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(name)) {
reader.close();
return line;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 根据姓名删除个人信息
private boolean deleteByName(String name) {
try {
BufferedReader reader = new BufferedReader(new FileReader("addressbook.txt"));
StringBuffer buffer = new StringBuffer();
String line;
boolean deleted = false;
while ((line = reader.readLine()) != null) {
if (line.startsWith(name)) {
deleted = true;
continue;
}
buffer.append(line + "\n");
}
reader.close();
if (deleted) {
FileWriter writer = new FileWriter("addressbook.txt");
writer.write(buffer.toString());
writer.close();
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
// 清空所有文本框中的信息
private void clearFields() {
nameField.setText("");
postCodeField.setText("");
addressField.setText("");
phoneField.setText("");
mobileField.setText("");
emailField.setText("");
}
// 在提示信息区域显示信息
private void showMessage(String message) {
JOptionPane.showMessageDialog(this, message, "提示信息", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new PersonalAddressBook();
}
}
```
阅读全文