写一个学生管理系统,包含图形化界面,用java语言实现
时间: 2024-03-24 14:41:17 浏览: 65
好的,我可以为您提供一个基本的学生管理系统的代码框架,您可以在此基础上进行修改和完善,以下是代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class StudentManagementSystem extends JFrame implements ActionListener{
//定义组件
JLabel label1,label2,label3,label4,label5,label6;
JTextField text1,text2,text3,text4,text5,text6;
JButton btn1,btn2,btn3,btn4;
JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7;
JTable table;
JScrollPane sp;
//定义数据模型
DefaultTableModel model;
//定义数据容器
Vector rowData,columnNames;
//定义学生列表
ArrayList<Student> stuList;
public StudentManagementSystem(){
//初始化组件
label1 = new JLabel("学号");
label2 = new JLabel("姓名");
label3 = new JLabel("性别");
label4 = new JLabel("年龄");
label5 = new JLabel("班级");
label6 = new JLabel("电话");
text1 = new JTextField();
text2 = new JTextField();
text3 = new JTextField();
text4 = new JTextField();
text5 = new JTextField();
text6 = new JTextField();
btn1 = new JButton("添加");
btn2 = new JButton("修改");
btn3 = new JButton("删除");
btn4 = new JButton("清空");
panel1 = new JPanel(new GridLayout(1,1));
panel2 = new JPanel(new GridLayout(1,1));
panel3 = new JPanel(new GridLayout(1,1));
panel4 = new JPanel(new GridLayout(1,1));
panel5 = new JPanel(new GridLayout(1,1));
panel6 = new JPanel(new GridLayout(1,1));
panel7 = new JPanel(new GridLayout(2,2));
//添加组件
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
panel3.add(label3);
panel3.add(text3);
panel4.add(label4);
panel4.add(text4);
panel5.add(label5);
panel5.add(text5);
panel6.add(label6);
panel6.add(text6);
panel7.add(btn1);
panel7.add(btn2);
panel7.add(btn3);
panel7.add(btn4);
//添加事件监听
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
//设置表格
rowData = new Vector();
columnNames = new Vector();
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("班级");
columnNames.add("电话");
model = new DefaultTableModel(rowData,columnNames);
table = new JTable(model);
sp = new JScrollPane(table);
//设置布局
this.setLayout(new GridLayout(4,1));
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.add(panel5);
this.add(panel6);
this.add(panel7);
this.add(sp);
//初始化学生列表
stuList = new ArrayList<Student>();
//设置窗口属性
this.setTitle("学生管理系统");
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//添加学生
public void addStudent(){
String id = text1.getText();
String name = text2.getText();
String gender = text3.getText();
String age = text4.getText();
String clazz = text5.getText();
String phone = text6.getText();
Student stu = new Student(id,name,gender,age,clazz,phone);
stuList.add(stu);
refreshTable();
clearInput();
}
//修改学生
public void modifyStudent(){
int row = table.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(null, "请先选择要修改的学生!");
return;
}
String id = text1.getText();
String name = text2.getText();
String gender = text3.getText();
String age = text4.getText();
String clazz = text5.getText();
String phone = text6.getText();
Student stu = stuList.get(row);
stu.setId(id);
stu.setName(name);
stu.setGender(gender);
stu.setAge(age);
stu.setClazz(clazz);
stu.setPhone(phone);
refreshTable();
clearInput();
}
//删除学生
public void deleteStudent(){
int row = table.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(null, "请先选择要删除的学生!");
return;
}
stuList.remove(row);
refreshTable();
clearInput();
}
//清空输入框
public void clearInput(){
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
}
//刷新表格数据
public void refreshTable(){
model.setRowCount(0);
for(Student stu : stuList){
Vector rowData = new Vector();
rowData.add(stu.getId());
rowData.add(stu.getName());
rowData.add(stu.getGender());
rowData.add(stu.getAge());
rowData.add(stu.getClazz());
rowData.add(stu.getPhone());
model.addRow(rowData);
}
}
//事件处理
public void actionPerformed(ActionEvent e){
if(e.getSource() == btn1){
addStudent();
}else if(e.getSource() == btn2){
modifyStudent();
}else if(e.getSource() == btn3){
deleteStudent();
}else if(e.getSource() == btn4){
clearInput();
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
//学生类
class Student{
private String id;
private String name;
private String gender;
private String age;
private String clazz;
private String phone;
public Student(String id, String name, String gender, String age, String clazz, String phone){
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.clazz = clazz;
this.phone = phone;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
以上代码包含了一个基本的学生管理系统的实现,包含了添加、修改、删除和清空操作。您可以根据自己的需求进行修改和完善。
阅读全文