用java写一个电话号码管理及查询程序,程序界面为GUI
时间: 2024-03-26 08:38:00 浏览: 23
好的,下面是一个简单的电话号码管理及查询程序的Java代码示例,程序界面使用了Swing库,你可以参考:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class PhoneBookGUI extends JFrame {
private Map<String, String> phoneBook = new HashMap<String, String>();
private JTextField nameField = new JTextField(10);
private JTextField phoneField = new JTextField(10);
public PhoneBookGUI() {
setTitle("电话号码管理程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
JPanel inputPanel = new JPanel(new GridLayout(2, 2));
inputPanel.add(new JLabel("姓名:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("电话:"));
inputPanel.add(phoneField);
JButton addButton = new JButton("添加联系人");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String phoneNumber = phoneField.getText();
phoneBook.put(name, phoneNumber);
JOptionPane.showMessageDialog(null, "联系人添加成功!");
nameField.setText("");
phoneField.setText("");
}
});
JButton searchButton = new JButton("查询联系人");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String searchName = nameField.getText();
String searchPhoneNumber = phoneBook.get(searchName);
if (searchPhoneNumber == null) {
JOptionPane.showMessageDialog(null, "该联系人不存在!");
} else {
JOptionPane.showMessageDialog(null, searchName + " 的电话是:" + searchPhoneNumber);
}
nameField.setText("");
phoneField.setText("");
}
});
JButton deleteButton = new JButton("删除联系人");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String deleteName = nameField.getText();
if (phoneBook.remove(deleteName) != null) {
JOptionPane.showMessageDialog(null, deleteName + " 的联系信息已删除!");
} else {
JOptionPane.showMessageDialog(null, "该联系人不存在!");
}
nameField.setText("");
phoneField.setText("");
}
});
JButton exitButton = new JButton("退出程序");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
buttonPanel.add(deleteButton);
buttonPanel.add(exitButton);
add(inputPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
PhoneBookGUI phoneBook = new PhoneBookGUI();
phoneBook.setVisible(true);
}
}
```
这个程序使用了JFrame、JPanel、JTextField、JButton和JOptionPane等Swing组件,用户可以通过输入不同的操作选项来实现添加、查询、删除联系人等功能,并且程序界面使用了GUI。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)