java利用awt和swing实现通讯录管理系统
时间: 2023-09-22 10:06:04 浏览: 179
要实现通讯录管理系统,可以使用 Java 的 AWT(Abstract Window Toolkit)和 Swing 库来设计用户界面。以下是一个简单的示例:
1. 首先,创建一个 JFrame 窗口,设置其大小和布局:
```
import javax.swing.*;
import java.awt.*;
public class AddressBook extends JFrame {
public AddressBook() {
setTitle("Address Book");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
}
}
```
2. 在 JFrame 窗口中添加一个 JPanel,用于放置通讯录条目。使用 JScrollPane 包装 JPanel,以便在需要时可以滚动:
```
import javax.swing.*;
import java.awt.*;
public class AddressBook extends JFrame {
public AddressBook() {
setTitle("Address Book");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(contentPane);
add(scrollPane, BorderLayout.CENTER);
}
}
```
3. 创建一个通讯录条目的类 AddressEntry,包括姓名、电话和电子邮件等信息:
```
public class AddressEntry {
private String name;
private String phoneNumber;
private String email;
public AddressEntry(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
}
```
4. 在 JPanel 中添加一个按钮,用于添加新的通讯录条目。创建一个对话框,让用户输入新条目的信息:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddressBook extends JFrame {
private JPanel contentPane;
public AddressBook() {
setTitle("Address Book");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(contentPane);
add(scrollPane, BorderLayout.CENTER);
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Create a dialog to get the new entry information
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JTextField nameField = new JTextField();
JTextField phoneField = new JTextField();
JTextField emailField = new JTextField();
panel.add(new JLabel("Name:"));
panel.add(nameField);
panel.add(new JLabel("Phone:"));
panel.add(phoneField);
panel.add(new JLabel("Email:"));
panel.add(emailField);
int result = JOptionPane.showConfirmDialog(null, panel, "Add New Entry", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// Add the new entry to the content pane
AddressEntry entry = new AddressEntry(nameField.getText(), phoneField.getText(), emailField.getText());
addEntry(entry);
}
}
});
add(addButton, BorderLayout.SOUTH);
}
private void addEntry(AddressEntry entry) {
JLabel label = new JLabel(entry.getName() + " " + entry.getPhoneNumber() + " " + entry.getEmail());
contentPane.add(label);
contentPane.revalidate();
contentPane.repaint();
}
}
```
5. 启动应用程序,显示通讯录管理系统窗口:
```
public class Main {
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
addressBook.setVisible(true);
}
}
```
这是一个基本的通讯录管理系统,可以通过添加新的通讯录条目来扩展功能。当然,这只是一个简单的示例,真正的通讯录管理系统可能需要更复杂的功能和更多的界面元素。
阅读全文