javaswing快递管理系统
时间: 2023-06-23 17:55:57 浏览: 171
下面是一个简单的 Java Swing 快递管理系统的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ExpressManagementSystem extends JFrame {
private JLabel nameLabel;
private JTextField nameField;
private JLabel phoneLabel;
private JTextField phoneField;
private JLabel addressLabel;
private JTextField addressField;
private JButton addButton;
private DefaultListModel<String> expressModel;
private JList<String> expressList;
public ExpressManagementSystem() {
setTitle("快递管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建姓名标签和文本框
nameLabel = new JLabel("姓名:");
nameField = new JTextField(10);
// 创建手机号码标签和文本框
phoneLabel = new JLabel("手机号码:");
phoneField = new JTextField(10);
// 创建地址标签和文本框
addressLabel = new JLabel("地址:");
addressField = new JTextField(30);
// 创建添加按钮
addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String phone = phoneField.getText();
String address = addressField.getText();
if (name.trim().length() > 0 && phone.trim().length() > 0 && address.trim().length() > 0) {
String express = name + " " + phone + " " + address;
expressModel.addElement(express);
nameField.setText("");
phoneField.setText("");
addressField.setText("");
} else {
JOptionPane.showMessageDialog(ExpressManagementSystem.this, "请填写完整信息!");
}
}
});
// 创建快递列表
expressModel = new DefaultListModel<>();
expressList = new JList<>(expressModel);
expressList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 将组件添加到窗口中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(phoneLabel);
inputPanel.add(phoneField);
inputPanel.add(addressLabel);
inputPanel.add(addressField);
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(expressList), BorderLayout.CENTER);
contentPane.add(addButton, BorderLayout.SOUTH);
// 设置窗口大小并居中显示
setSize(500, 300);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
ExpressManagementSystem expressManagementSystem = new ExpressManagementSystem();
expressManagementSystem.setVisible(true);
}
}
```
这个程序界面包含三个输入框(姓名、手机号码、地址)、一个添加按钮和一个快递列表。当用户在输入框中填写完整信息并点击添加按钮时,程序会将快递信息添加到快递列表中。
阅读全文