创建一个宿舍管理项目的图形化界面的Java代码 能够运行出来界面的代码
时间: 2024-03-05 17:49:26 浏览: 44
以下是一个简单的宿舍管理项目的图形化界面的Java代码,可以运行出来一个基本的界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DormitoryManagementSystem extends JFrame implements ActionListener {
private JLabel titleLabel, nameLabel, ageLabel, genderLabel, dormitoryLabel;
private JTextField nameField, ageField, dormitoryField;
private JComboBox genderBox;
private JButton addButton, deleteButton, modifyButton, searchButton;
private JTable studentTable;
public DormitoryManagementSystem() {
setTitle("宿舍管理系统");
setSize(800, 600);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 标题
titleLabel = new JLabel("宿舍管理系统", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 28));
add(titleLabel, BorderLayout.NORTH);
// 学生信息
JPanel studentPanel = new JPanel();
studentPanel.setLayout(new GridLayout(4, 2, 10, 10));
studentPanel.setBorder(BorderFactory.createTitledBorder("学生信息"));
nameLabel = new JLabel("姓名:");
nameField = new JTextField();
ageLabel = new JLabel("年龄:");
ageField = new JTextField();
genderLabel = new JLabel("性别:");
String[] genders = {"男", "女"};
genderBox = new JComboBox(genders);
dormitoryLabel = new JLabel("宿舍号:");
dormitoryField = new JTextField();
studentPanel.add(nameLabel);
studentPanel.add(nameField);
studentPanel.add(ageLabel);
studentPanel.add(ageField);
studentPanel.add(genderLabel);
studentPanel.add(genderBox);
studentPanel.add(dormitoryLabel);
studentPanel.add(dormitoryField);
add(studentPanel, BorderLayout.WEST);
// 操作按钮
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 1, 10, 10));
addButton = new JButton("添加");
addButton.addActionListener(this);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
modifyButton = new JButton("修改");
modifyButton.addActionListener(this);
searchButton = new JButton("查询");
searchButton.addActionListener(this);
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(modifyButton);
buttonPanel.add(searchButton);
add(buttonPanel, BorderLayout.CENTER);
// 学生列表
JPanel tablePanel = new JPanel();
tablePanel.setBorder(BorderFactory.createTitledBorder("学生列表"));
String[] columnNames = {"ID", "姓名", "年龄", "性别", "宿舍号"};
Object[][] data = {};
studentTable = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(studentTable);
tablePanel.add(scrollPane);
add(tablePanel, BorderLayout.EAST);
setVisible(true);
}
public static void main(String[] args) {
new DormitoryManagementSystem();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 处理按钮事件
}
}
```
这段代码创建了一个包含学生信息、操作按钮和学生列表的宿舍管理系统界面。您可以根据实际需求进行修改和完善。
阅读全文