编写javagui学生管理系统
时间: 2023-10-17 12:20:15 浏览: 85
JAVA-GUI学生管理系统
5星 · 资源好评率100%
下面是一个简单的Java GUI学生管理系统示例,可以添加、修改、删除和搜索学生信息。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class StudentManagementSystem extends JFrame implements ActionListener{
private JLabel nameLabel, idLabel, ageLabel, genderLabel, departmentLabel, searchLabel;
private JTextField nameTextField, idTextField, ageTextField, searchTextField;
private JComboBox genderComboBox, departmentComboBox;
private JButton addButton, updateButton, deleteButton, searchButton;
private JTable table;
private DefaultTableModel model;
private JScrollPane scrollPane;
private ArrayList<Student> studentList;
public StudentManagementSystem(){
setTitle("学生管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nameLabel = new JLabel("姓名:");
idLabel = new JLabel("学号:");
ageLabel = new JLabel("年龄:");
genderLabel = new JLabel("性别:");
departmentLabel = new JLabel("院系:");
searchLabel = new JLabel("搜索:");
nameTextField = new JTextField();
idTextField = new JTextField();
ageTextField = new JTextField();
searchTextField = new JTextField();
genderComboBox = new JComboBox();
genderComboBox.addItem("男");
genderComboBox.addItem("女");
departmentComboBox = new JComboBox();
departmentComboBox.addItem("计算机科学与技术");
departmentComboBox.addItem("软件工程");
departmentComboBox.addItem("网络工程");
addButton = new JButton("添加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
searchButton = new JButton("搜索");
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
searchButton.addActionListener(this);
Object[] columns = {"姓名", "学号", "年龄", "性别", "院系"};
model = new DefaultTableModel();
model.setColumnIdentifiers(columns);
table = new JTable(model);
scrollPane = new JScrollPane(table);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(nameLabel)
.addComponent(idLabel)
.addComponent(ageLabel)
.addComponent(genderLabel)
.addComponent(departmentLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(nameTextField)
.addComponent(idTextField)
.addComponent(ageTextField)
.addComponent(genderComboBox)
.addComponent(departmentComboBox)
.addComponent(addButton)
.addComponent(updateButton)
.addComponent(deleteButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(searchLabel)
.addComponent(searchTextField)
.addComponent(searchButton))
.addComponent(scrollPane)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(nameLabel)
.addComponent(nameTextField)
.addComponent(searchLabel)
.addComponent(searchTextField))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(idLabel)
.addComponent(idTextField)
.addComponent(searchButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ageLabel)
.addComponent(ageTextField))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(genderLabel)
.addComponent(genderComboBox))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(departmentLabel)
.addComponent(departmentComboBox))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(addButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(updateButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(deleteButton))
.addComponent(scrollPane)
);
pack();
setLocationRelativeTo(null);
setVisible(true);
studentList = new ArrayList<Student>();
}
public static void main(String[] args) {
new StudentManagementSystem();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addButton){
String name = nameTextField.getText();
String id = idTextField.getText();
int age = Integer.parseInt(ageTextField.getText());
String gender = genderComboBox.getSelectedItem().toString();
String department = departmentComboBox.getSelectedItem().toString();
Student student = new Student(name, id, age, gender, department);
studentList.add(student);
Object[] row = new Object[5];
row[0] = student.getName();
row[1] = student.getId();
row[2] = student.getAge();
row[3] = student.getGender();
row[4] = student.getDepartment();
model.addRow(row);
}
else if(e.getSource() == updateButton){
int i = table.getSelectedRow();
String name = nameTextField.getText();
String id = idTextField.getText();
int age = Integer.parseInt(ageTextField.getText());
String gender = genderComboBox.getSelectedItem().toString();
String department = departmentComboBox.getSelectedItem().toString();
Student student = studentList.get(i);
student.setName(name);
student.setId(id);
student.setAge(age);
student.setGender(gender);
student.setDepartment(department);
model.setValueAt(name, i, 0);
model.setValueAt(id, i, 1);
model.setValueAt(age, i, 2);
model.setValueAt(gender, i, 3);
model.setValueAt(department, i, 4);
}
else if(e.getSource() == deleteButton){
int i = table.getSelectedRow();
studentList.remove(i);
model.removeRow(i);
}
else if(e.getSource() == searchButton){
String searchText = searchTextField.getText();
for(int i=0; i<model.getRowCount(); i++){
String name = model.getValueAt(i, 0).toString();
String id = model.getValueAt(i, 1).toString();
if(name.contains(searchText) || id.contains(searchText)){
table.setRowSelectionInterval(i, i);
break;
}
}
}
}
class Student {
private String name;
private String id;
private int age;
private String gender;
private String department;
public Student(String name, String id, int age, String gender, String department) {
this.name = name;
this.id = id;
this.age = age;
this.gender = gender;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
}
```
注意:这只是一个简单的示例,实际的学生管理系统可能需要更多的功能和复杂性。
阅读全文