帮我写一个用java写的有UI的学生管理系统
时间: 2024-01-02 07:04:20 浏览: 70
好的,下面是一个简单的Java学生信息管理系统,具有UI界面,可以实现学生信息的增删改查功能。
代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
public class StudentManagementSystem extends JFrame {
private JTextField idField, nameField, ageField;
private JButton addButton, deleteButton, updateButton, searchButton;
private JTextArea resultArea;
private ArrayList<Student> students;
public StudentManagementSystem() {
super("学生管理系统");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化UI界面
initUI();
// 加载学生信息
loadStudents();
}
private void initUI() {
// 创建输入框和按钮
idField = new JTextField(10);
nameField = new JTextField(10);
ageField = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("更新");
searchButton = new JButton("查询");
// 添加按钮的事件监听器
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateStudent();
}
});
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchStudent();
}
});
// 创建结果显示区域
resultArea = new JTextArea();
resultArea.setEditable(false);
// 创建UI布局并添加组件
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
panel.add(new JLabel("学号:"), c);
c.gridx = 1;
panel.add(idField, c);
c.gridx = 0;
c.gridy = 1;
panel.add(new JLabel("姓名:"), c);
c.gridx = 1;
panel.add(nameField, c);
c.gridx = 0;
c.gridy = 2;
panel.add(new JLabel("年龄:"), c);
c.gridx = 1;
panel.add(ageField, c);
c.gridx = 2;
c.gridy = 0;
panel.add(addButton, c);
c.gridy = 1;
panel.add(deleteButton, c);
c.gridy = 2;
panel.add(updateButton, c);
c.gridx = 3;
c.gridy = 0;
panel.add(searchButton, c);
// 将组件添加到窗口中
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(resultArea), BorderLayout.CENTER);
}
private void loadStudents() {
students = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader("students.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
int id = Integer.parseInt(fields[0]);
String name = fields[1];
int age = Integer.parseInt(fields[2]);
students.add(new Student(id, name, age));
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "读取文件失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
updateResultArea();
}
private void saveStudents() {
try {
PrintWriter writer = new PrintWriter(new FileWriter("students.txt"));
for (Student student : students) {
writer.println(student.getId() + "," + student.getName() + "," + student.getAge());
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "保存文件失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void addStudent() {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
Student student = new Student(id, name, age);
students.add(student);
saveStudents();
updateResultArea();
}
private void deleteStudent() {
int id = Integer.parseInt(idField.getText());
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
break;
}
}
saveStudents();
updateResultArea();
}
private void updateStudent() {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.set(i, new Student(id, name, age));
break;
}
}
saveStudents();
updateResultArea();
}
private void searchStudent() {
int id = Integer.parseInt(idField.getText());
for (Student student : students) {
if (student.getId() == id) {
resultArea.setText(student.toString());
return;
}
}
JOptionPane.showMessageDialog(this, "未找到该学生!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
private void updateResultArea() {
resultArea.setText("");
for (Student student : students) {
resultArea.append(student.toString() + "\n");
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.setVisible(true);
}
}
class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age;
}
}
```
该程序使用了Java Swing库实现了UI界面,可以添加、删除、更新和查询学生信息,学生信息以文本文件的形式保存在本地。
需要注意的是,该程序只是一个简单的示例,实际应用中可能需要更多的功能和更复杂的UI界面。
阅读全文