java 编写电话号码查询的gui程序
时间: 2024-05-06 21:17:43 浏览: 22
下面是一个简单的Java电话号码查询的GUI程序示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PhoneBookGUI extends JFrame implements ActionListener {
private JTextField nameField, phoneField;
private JButton addButton, searchButton;
private JTextArea displayArea;
private PhoneBook phoneBook;
public PhoneBookGUI() {
super("Phone Book");
// Create the phone book
phoneBook = new PhoneBook();
// Set up the GUI components
nameField = new JTextField(10);
phoneField = new JTextField(10);
addButton = new JButton("Add");
searchButton = new JButton("Search");
displayArea = new JTextArea(10, 20);
// Add action listeners to the buttons
addButton.addActionListener(this);
searchButton.addActionListener(this);
// Create the panel for the input fields and buttons
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
inputPanel.add(new JLabel("Name:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("Phone:"));
inputPanel.add(phoneField);
inputPanel.add(addButton);
inputPanel.add(searchButton);
// Create the panel for the display area
JScrollPane scrollPane = new JScrollPane(displayArea);
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new BorderLayout());
displayPanel.add(scrollPane, BorderLayout.CENTER);
// Add the panels to the content pane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(displayPanel, BorderLayout.CENTER);
// Set the size and make the GUI visible
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// Add a phone number to the phone book
String name = nameField.getText();
String phone = phoneField.getText();
phoneBook.addPhoneNumber(name, phone);
displayArea.setText("Phone number added.");
nameField.setText("");
phoneField.setText("");
} else if (e.getSource() == searchButton) {
// Search for a phone number in the phone book
String name = nameField.getText();
String phone = phoneBook.getPhoneNumber(name);
if (phone != null) {
displayArea.setText("Phone number: " + phone);
} else {
displayArea.setText("Phone number not found.");
}
phoneField.setText("");
}
}
public static void main(String[] args) {
PhoneBookGUI phoneBookGUI = new PhoneBookGUI();
phoneBookGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class PhoneBook {
private java.util.Map<String, String> phoneNumbers;
public PhoneBook() {
phoneNumbers = new java.util.HashMap<String, String>();
}
public void addPhoneNumber(String name, String phone) {
phoneNumbers.put(name, phone);
}
public String getPhoneNumber(String name) {
return phoneNumbers.get(name);
}
}
```
该程序有一个简单的GUI,包含两个文本框和两个按钮。用户可以输入一个人的姓名和电话号码,然后按下“Add”按钮将该信息添加到电话簿中。用户还可以输入一个人的姓名,并按下“Search”按钮来查找该人的电话号码。如果电话号码被找到,它将在文本区域中显示,否则将显示“Phone number not found.”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)