swing应用(三):简易学生管理系统 刺骨的言语ヽ痛彻心扉 2023-01-01 14:53 114阅读 0赞 ## 要求 ## **目标:** 设计一个学生成绩管理系统,能够进行简单的学生成绩输入,查询统计等功能。 **设计任务:** 1. 实现定义学生成绩记录,记录包括:学生姓名,性别,学号,课程名,成绩,平均分。 2. 实现学生成绩管理查询系统的菜单管理系统的功能,能查看,添加,修改,删除,统计等操作。 3. 完成查看学生成绩单的功能,此功能可显示所有学生成绩。 4. 实现添加学生成绩记录功能,输入学生成绩并记录到成绩表中。 5. 实现修改学生成绩记录功能,根据学生学号修改或删除课程成绩。 6. 实现统计某门课平均分的功能。 7. 实现按学号查找某位学生成绩的功能。 ## 实现 ## **1、学生类** class Student{ private String no; private String name; private String age;//所属班级 private String score1; private String score2; private String score3; private String score4; public Student(String no, String name, String age,String score1,String score2,String score3,String score4) { this.no = no; this.name = name; this.age = age; this.score1 = score1; this.score2 = score2; this.score3 = score3; this.score4 = score4; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getScore1() { return score1; } public void setScore1(String score1) { this.score1 = score1; } public String getScore2() { return score2; } public void setScore2(String score2) { this.score2 = score2; } public String getScore3() { return score3; } public void setScore3(String score3) { this.score3 = score3; } public String getScore4() { return score4; } public void setScore4(String score4) { this.score4 = score4; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(String age){ this.age=age; } public String getAge(){ return age; } } **2、学生集合类** class Class{ ArrayList<Student> stuList; public Class(){ this.stuList=new ArrayList<>(); } public void addStudent(Student stu){ this.stuList.add(stu); } public void deleteStudent(Student s){ this.stuList.remove(s); } } **3、集合转数组工具类** import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class ListToArray { /** * 获取对象属性,返回一个字符串数组 * @param o 对象 * @return String[] 字符串数组 */ private String[] getFiledName(Object o) { try { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { fieldNames[i] = fields[i].getName(); } return fieldNames; } catch (SecurityException e) { e.printStackTrace(); System.out.println(e.toString()); } return null; } /** * 使用反射根据属性名称获取t属性的get方法 * @param fieldName 属性名称 * @param o 操作对象 * @return List<Method> get方法 */ private List<Method> getGetField(String[] fieldNames, Object o) { List<Method> methods=new ArrayList<Method>(); for (String fieldName : fieldNames) { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = "get" + firstLetter + fieldName.substring(1); Method method = null; try { method = o.getClass().getMethod(getter); } catch (NoSuchMethodException e) { System.out.println("属性不存在"); continue; } //Object value = method.invoke(o, new Object[] {}); methods.add(method); } return methods; } /** * 将list集合转换为二维string数组 * @param list 要转换的集合 * @return String[][] 返回的sting数组 */ public String[][] listToArrayWay(List list) { Object o=list.get(0); String[] filedNames = getFiledName(o); int filedNum=filedNames.length; int listSize=list.size(); List<Method> methods=getGetField(filedNames, o); String[][] arrs=new String[listSize][filedNum]; int i=0; for (Object object : list) { int j=0; for (Method method : methods) { Object value=null; try { value = method.invoke(object, new Object[] { }); } catch (Exception e) { System.out.println("属性不存在"+e); } arrs[i][j]=(String) value; j++; } i++; } return arrs; } } **4、登录窗体类** import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; class menu extends JFrame{ JTextField txtyonghu; JTextField txtmima; public menu() { this.setBounds(300, 150, 500, 450); this.setTitle("学生信息管理系统"); this.setLayout(null);//取消JPanel的布局(避免JPanel的setLayout布局与setBounds方法相冲突) JLabel labTipw = new JLabel("欢迎进入学生信息管理系统"); labTipw.setBounds(50, 2, 250, 45); JLabel labTipws = new JLabel("(*新用户初始密码为 身份证后五位数字)"); labTipws.setBounds(50, 15, 250, 45); JLabel labyonghu = new JLabel("用户名:"); labyonghu.setBounds(50, 60, 100, 50); txtyonghu = new JTextField("请输入登录用户名:",30); txtyonghu.setBounds(130, 76, 180, 30); JLabel labmima = new JLabel("密码:"); labmima.setBounds(50, 100, 50, 50); txtmima= new JTextField("请输入登录密码:",30); txtmima.setBounds(130, 117, 180, 30); JButton btndianji = new JButton("点击登录"); btndianji.setBounds(130, 165, 180, 30); btndianji.setForeground(Color.BLUE); this.add(labTipw); this.add(labTipws); this.add(labyonghu); this.add(txtyonghu); this.add(labmima); this.add(txtmima); this.add(btndianji); btndianji.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //获取用户名 String username=txtyonghu.getText(); //获取密码 String password=txtmima.getText(); if ("admin".equals(username)&&"admin".equals(password)) { new StudentFrame(); }else{ JOptionPane.showMessageDialog(null, "用户名或密码错误"); } } }); setVisible(true); } } **5、表格类** import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; public class StudentTable { public StudentTable(List<Student> list){ //定义列名与行数据,其中列名最好用final修饰 final Object[] columnNames = { "学号","姓名", "班级", "java","c语言", "python","c++"}; //将list集合转为二维数组 ListToArray tools=new ListToArray(); JTable table1 = new JTable (tools.listToArrayWay(list),columnNames);//12行6列的空表格 //创建窗口中将要用到的面板 JScrollPane pane1 = new JScrollPane (table1); JPanel pan = new JPanel (new GridLayout (0, 1)); pan.setPreferredSize (new Dimension (600,250)); pan.setBackground (Color.black); pan.add (pane1); //创建窗口 JFrame frm = new JFrame ("学生成绩表"); frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭子窗体的时候不会自动关闭父窗体 frm.setContentPane (pan); frm.pack(); frm.setVisible(true); } } 6、主体类 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; public class StudentFrame extends JFrame { Class c = new Class(); JLabel labNo; JLabel labName; JLabel labAge; JLabel labscore1; JLabel labscore2; JLabel labscore3; JLabel labscore4; JLabel labscore5; JLabel labTip; JLabel labavr; JTextField txtNo; JTextField txtName; JTextField txtAge; JTextField txtscore1; JTextField txtscore2; JTextField txtscore3; JTextField txtscore4; JTextField txtscore5; JTextField txtavr; //JButton btnaver; JButton btnAdd; JButton btnRemove; JButton btnModify; JButton btnClear; JButton btnFirst; JButton btnPrev; JButton btnNext; JButton btnLast; JButton btnGrade; JButton btnCheck; JOptionPane dialog; public StudentFrame() { this.setBounds(300, 100, 650, 530); this.setTitle("学生信息管理系统"); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); studentin(); addStu(); firstStudent(); lastStudent(); previous(); next(); remove(); check(); modify(); clear(); //aver(); gradeList(); this.setVisible(true); } public void studentin() { labTip = new JLabel("登陆成功 !您已经进入管理系统(仅输入学号便可查找学生信息)."); labTip.setBounds(50, 2, 550, 45); labNo = new JLabel("学生学号:"); labNo.setBounds(50, 20, 100, 50); txtNo = new JTextField("请输入6位学号:",30); txtNo.setBounds(130, 35, 180, 30); labName = new JLabel("学生姓名:"); labName.setBounds(50, 60, 100, 50); txtName = new JTextField("请输入学生姓名:",30); txtName.setBounds(130, 76, 180, 30); labAge = new JLabel("班级:"); labAge.setBounds(50, 100, 50, 50); txtAge = new JTextField("请输入学生所在班级:",30); txtAge.setBounds(130, 117, 180, 30); labscore1 = new JLabel("JAVA 成绩:"); labscore1.setBounds(50, 140, 100, 50); txtscore1 = new JTextField("请输入学生成绩:",30); txtscore1.setBounds(130, 158, 180, 30); labscore2 = new JLabel("C语言成绩:"); labscore2.setBounds(50, 189, 100, 50); txtscore2 = new JTextField("请输入学生成绩:",30); txtscore2.setBounds(130, 199, 180, 30); labscore3 = new JLabel("Python成绩:"); labscore3.setBounds(50, 230, 100, 50); txtscore3 = new JTextField("请输入学生成绩:",30); txtscore3.setBounds(130, 240, 180, 30); labscore4 = new JLabel("C++ 成绩:"); labscore4.setBounds(50, 290, 100, 50); txtscore4 = new JTextField("请输入学生成绩:",30); txtscore4.setBounds(130, 301, 180, 30); labscore5 = new JLabel("java平均成绩="); labscore5.setBounds(330,290,100,50); txtscore5 = new JTextField("java平均成绩",30); txtscore5.setBounds(420,301,180,30); labavr = new JLabel("操作说明 ** |<< : 第一个学生信息 <:上一位学生信息 >:下一个学生信息 >>|:最后一个学生信息"); labavr.setBounds(50, 330, 580, 50); btnAdd = new JButton("添加"); btnAdd.setBounds(50, 385, 80, 20); btnAdd.setForeground(Color.BLACK); btnModify = new JButton("修改"); btnModify.setBounds(150, 385, 80, 20); btnModify.setForeground(Color.BLUE); btnRemove = new JButton("删除"); btnRemove.setBounds(250, 385, 80, 20); btnRemove.setForeground(Color.RED); btnClear = new JButton("清空"); btnClear.setBounds(350, 385, 80, 20); btnClear.setForeground(Color.GREEN); btnGrade = new JButton("成绩单"); btnGrade.setBounds(350, 60, 160, 50); btnGrade.setForeground(Color.BLACK); btnCheck = new JButton("查找信息"); btnCheck.setBounds(350, 145, 160, 50); btnCheck.setForeground(Color.BLACK); btnFirst = new JButton("|<<"); btnFirst.setBounds(50, 432, 80, 20); btnFirst.setForeground(Color.PINK); btnPrev = new JButton("<"); btnPrev.setBounds(150, 432, 80, 20); btnPrev.setForeground(Color.BLACK); btnNext = new JButton(">"); btnNext.setBounds(250, 432, 80, 20); btnNext.setForeground(Color.BLACK); btnLast = new JButton(">>|"); btnLast.setBounds(350, 432, 80, 20); btnLast.setForeground(Color.PINK); /*btnaver = new JButton("平均成绩"); btnaver.setBounds(350, 220, 150, 50); btnaver.setForeground(Color.BLACK);*/ this.add(labNo); this.add(labName); this.add(labAge); this.add(labscore1); this.add(txtNo); this.add(labTip); this.add(txtName); this.add(txtAge); this.add(txtscore1); this.add(btnAdd); this.add(btnRemove); this.add(btnModify); this.add(btnClear); this.add(btnFirst); this.add(btnPrev); this.add(btnNext); this.add(btnLast); this.add(btnGrade); this.add(btnCheck); //this.add(btnaver); this.add(labavr); this.add(labscore2); this.add(txtscore2); this.add(labscore3); this.add(txtscore3); this.add(labscore4); this.add(txtscore4); this.add(labscore5); this.add(txtscore5); } public boolean blank() { if (txtNo.getText().equals("")) { dialog.showMessageDialog(null, "学号输入有误 请重新输入!"); return false; } if (txtName.getText().equals("")) { dialog.showMessageDialog(null, "名字输入有误 请重新输入!"); return false; } if (txtAge.getText().equals("")) { dialog.showMessageDialog(null, "班级输入有误 请重新输入!"); return false; }if (txtscore1.getText().equals("")) { dialog.showMessageDialog(null, "成绩输入有误 请重新输入!"); return false; }if (txtscore2.getText().equals("")) { dialog.showMessageDialog(null, "成绩输入有误 请重新输入!"); return false; } if (txtscore3.getText().equals("")) { dialog.showMessageDialog(null, "成绩输入有误 请重新输入!"); return false; } if (txtscore4.getText().equals("")) { dialog.showMessageDialog(null, "成绩输入有误 请重新输入!"); return false; } return true; } public void addStu() { btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (blank() == true) { c.addStudent(new Student(txtNo.getText(), txtName.getText(), txtAge.getText(),txtscore1.getText(),txtscore2.getText(),txtscore3.getText(),txtscore4.getText())); dialog.showMessageDialog(null, "学生信息已添加成功"); } //查询java平均成绩 float sum=0; for (Student stu : c.stuList) { sum+=Integer.valueOf(stu.getScore1()); } if (!c.stuList.isEmpty()) { txtscore5.setText(sum/c.stuList.size()+""); } } }); } public void firstStudent() { btnFirst.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { txtNo.setText(c.stuList.get(0).getNo()); txtName.setText(c.stuList.get(0).getName()); txtAge.setText(c.stuList.get(0).getAge()); txtscore1.setText(c.stuList.get(0).getScore1()); txtscore2.setText(c.stuList.get(0).getScore2()); txtscore3.setText(c.stuList.get(0).getScore3()); txtscore4.setText(c.stuList.get(0).getScore4()); } }); } public void lastStudent() { btnLast.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { txtNo.setText(c.stuList.get(c.stuList.size() - 1).getNo()); txtName.setText(c.stuList.get(c.stuList.size() - 1).getName()); txtAge.setText(c.stuList.get(c.stuList.size() - 1).getAge()); txtscore1.setText(c.stuList.get(c.stuList.size() - 1).getScore1()); txtscore2.setText(c.stuList.get(c.stuList.size() - 1).getScore2()); txtscore3.setText(c.stuList.get(c.stuList.size() - 1).getScore3()); txtscore4.setText(c.stuList.get(c.stuList.size() - 1).getScore4()); } }); } public void previous() { btnPrev.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int n = 0; for (int i = 0; i < c.stuList.size(); i++) { if (c.stuList.get(i).getNo().equals(txtNo.getText())) { n = i - 1; } } if (c.stuList.get(0).getNo().equals(txtNo.getText())) { dialog.showMessageDialog(null, "再无上一个学生"); } else { txtNo.setText(c.stuList.get(n).getNo()); txtName.setText(c.stuList.get(n).getName()); txtAge.setText(c.stuList.get(n).getAge()); txtscore1.setText(c.stuList.get(n).getScore1()); txtscore2.setText(c.stuList.get(n).getScore2()); txtscore3.setText(c.stuList.get(n).getScore3()); txtscore4.setText(c.stuList.get(n).getScore4()); } } }); } public void next() { btnNext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int n = 0; for (int i = 0; i < c.stuList.size(); i++) { if (c.stuList.get(i).getNo().equals(txtNo.getText())) { n = i + 1; } } if (c.stuList.get(c.stuList.size() - 1).getNo().equals(txtNo.getText())) { dialog.showMessageDialog(null, "再无下一个学生"); } else { txtNo.setText(c.stuList.get(n).getNo()); txtName.setText(c.stuList.get(n).getName()); txtAge.setText(c.stuList.get(n).getAge()); txtscore1.setText(c.stuList.get(n).getScore1()); txtscore2.setText(c.stuList.get(n).getScore2()); txtscore3.setText(c.stuList.get(n).getScore3()); txtscore4.setText(c.stuList.get(n).getScore4());} } }); } public void remove() { btnRemove.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (c.stuList.size() == 0) { JOptionPane.showMessageDialog(null, "没有该学生信息"); } else { Student s = new Student(txtNo.getText(), txtName.getText(), txtAge.getText(),txtscore1.getText(),txtscore2.getText(),txtscore3.getText(),txtscore4.getText()); if (blank() == true) { System.out.println(c.stuList.contains(s)); System.out.println(s); if (!c.stuList.contains(s)) { c.deleteStudent(s); JOptionPane.showMessageDialog(null, "已经删除成功"); int a = 0; for (int i = 0; i < c.stuList.size(); i++) { if (c.stuList.get(i).getNo().equals(txtNo.getText())) { a = i + 1; } } if (c.stuList.size() > 0) { if (c.stuList.get(c.stuList.size() - 1).getNo().equals(txtNo.getText())) { txtNo.setText(c.stuList.get(0).getNo()); txtName.setText(c.stuList.get(0).getName()); txtAge.setText(c.stuList.get(0).getAge()); txtscore1.setText(c.stuList.get(0).getScore1()); txtscore2.setText(c.stuList.get(0).getScore2()); txtscore3.setText(c.stuList.get(0).getScore3()); txtscore4.setText(c.stuList.get(0).getScore4()); } else { txtNo.setText(c.stuList.get(a).getNo()); txtName.setText(c.stuList.get(a).getName()); txtAge.setText(c.stuList.get(a).getAge()); txtscore1.setText(c.stuList.get(a).getScore1()); txtscore2.setText(c.stuList.get(a).getScore2()); txtscore3.setText(c.stuList.get(a).getScore3()); txtscore4.setText(c.stuList.get(a).getScore4()); } } else { clear(); } } else { dialog.showMessageDialog(null, "未来找到对应学生的信息"); }} } } }); } public void modify() { btnModify.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (c.stuList.size() == 0) { dialog.showMessageDialog(null, "没有该学生信息"); } for (Student s : c.stuList) { if (s.getNo().equals(txtNo.getText())) { if (blank() == true) { s.setName(txtName.getText()); s.setAge(txtAge.getText()); JOptionPane.showMessageDialog(null, "已经修改成功"); return; } } } JOptionPane.showMessageDialog(null, "修改失败请重新操作"); } }); } public void check() { btnCheck.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if ((txtNo.getText().equals(""))) { dialog.showMessageDialog(null, "学号不能为空 请从新输入"); } else { int m = 0; for (int i = 0; i < c.stuList.size(); i++) { if (c.stuList.get(i).getNo().equals(txtNo.getText())) { JOptionPane.showMessageDialog(null, "已查到该学生信息"); txtName.setText(c.stuList.get(i).getName()); txtAge.setText(c.stuList.get(i).getAge()); txtscore1.setText(c.stuList.get(i).getScore1()); txtscore2.setText(c.stuList.get(i).getScore2()); txtscore3.setText(c.stuList.get(i).getScore3()); txtscore4.setText(c.stuList.get(i).getScore4()); break;} m++;} if (m == c.stuList.size()) { dialog.showMessageDialog(null, "没有此学生的相关信息"); return; } } } }); } /*public void aver() { btnaver.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "20"); } }); }*/ public void clear() { btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { txtNo.setText(""); txtName.setText(""); txtAge.setText(""); txtscore1.setText(""); txtscore2.setText(""); txtscore3.setText(""); txtscore4.setText(""); txtscore5.setText(""); } } ); } public void gradeList(){ btnGrade.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); if (!c.stuList.isEmpty()) { StudentTable studentTable=new StudentTable(c.stuList); }else{ JOptionPane.showMessageDialog(null, "无学生成绩记录"); } } }); } public static void main(String[] args) { new menu(); } 有问题可以私信或者在评论区留言。写作不易,既然来了,不妨点个关注,点个赞吧!!!
还没有评论,来说两句吧...