Java课程设计简单的班级管理系统.doc
Java 课程设计 -简单的班级管理系统 二:总体设计 本班级管理系统共有 2个java 源文件。 各个类之间的主要关系如下图所示: StudentManager ModiifyActInquesActShowActDeleteActInputActStudentShowStudent各主类的主要作用如下: 1. StudentManager.java StudentManager类该类负责创建班级学生管理系统的主窗口。该类包含 main 方法,程序从该类开始执行。该类嵌套如下类: 1).InputAct类该类负责创建班级学生管理系统的学生基本信息录入界面。 2).InquestAct类该类负责创建班级学生管理系统的学生基本信息查询界面。 3).ModifyAct类该类负责创建班级学生管理系统的学生基本信息修改界面。 4).DeleteAct类该类负责创建班级学生管理系统的学生基本信息删除界面。5).ShowAct类该类负责创建班级学生管理系统的学生基本信息显示界面。2.Student.java 负责创建存放学生信息的对象。三:详细设计 1. StudentManager.java StudentManager类 1).成员变量 主要成员变量 成员变量描述变量类型变量名称显示注释的标签JLabellb显示欢迎进入的标签JLabel欢迎设定字体属性Fontfont1输入学生基本信息的文本JTextField学号,姓名,专业,年级,出生,电话,住址性别选择JRadioButton男,女进行相应操作的按钮JButton录入,查询,删除,修改,显示,进入,确定,退出调整布局的通道JPanelp01,p02,p03,p04,p1,p2,p3,p4,p5,p6,p7,p8,p9,pv,ph,plb系统框架界面JFrameWelcome,operation存放学生基本信息的对象Student学生学生信息的散列表Hashtable基本信息存放散列表的文件Filefile2) . 方法主要方法名称功能备注StudentManager创建程序主窗口构造方法Main程序开始运行3) .内部类名称功能InputAct学生信息录入界面InquestAct学生信息查询界面ModifyAct学生信息修改界面DeleteAct学生信息删除界面ShowAct学生信息显示界面4) . 代码 StudentManager.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.*;public class StudentManager extends JFrame JLabel lb=new JLabel("注:录入请先输入记录,查询、删除请先输入学号,修改是对查询" +"内容改后的保存!"); JLabel 欢迎; Font font1=new Font("黑体",Font.BOLD,24); JTextField 学号,姓名,专业,年级,出生,电话,住址; JRadioButton 男,女; ButtonGroup group=null; JButton 录入,查询,删除,修改,显示,进入,确定,退出; JPanel p01,p02,p03,p04,p1,p2,p3,p4,p5,p6,p7,p8,p9,pv,ph,plb; Student 学生=null; Hashtable 学生散列表=null; File file=null; FileInputStream inOne=null; ObjectInputStream inTwo=null; FileOutputStream outOne=null; ObjectOutputStream outTwo=null; public StudentManager() final JFrame welcome = new JFrame(); final JFrame operation = new JFrame(); p01=new JPanel(); 欢迎=new JLabel("欢迎进入班级管理系统!"); 欢迎.setFont(font1); 进入=new JButton("进入"); 退出=new JButton("退出"); welcome.setLayout(new GridLayout(3,1,5,5); p01.add(欢迎); p02.add(进入); welcome.add(p03); welcome.add(p01); welcome.add(p02); welcome.setTitle("班级管理系统"); welcome.setSize(500,300); welcome.setVisible(true); 进入.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) welcome.setVisible(false); operation.setVisible(true); ); 退出.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.exit(0); ); operation.setTitle("班级管理系统"); 学号=new JTextField(10); group=new ButtonGroup(); 男=new JRadioButton("男",true); 女=new JRadioButton("女",false); group.add(男); group.add(女); 录入=new JButton("录入"); 录入.addActionListener(new InputAct(); 修改.setEnabled(false); 确定.setVisible(false); 确定.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) 姓名.setEnabled(true); 男.setEnabled(true); 女.setEnabled(true); 确定.setVisible(false); ); p1=new JPanel(); p1.add(new JLabel("学 号:",JLabel.CENTER); p1.add(学号); p9=new JPanel(); p9.add(确定); pv=new JPanel(); pv.setLayout(new GridLayout(10,1); pv.add(p04); pv.add(p1); pv.add(p9); ph=new JPanel(); ph.add(录入); ph.add(退出); plb=new JPanel(); plb.add(lb); file=new File("学生信息.txt"); 学生散列表=new Hashtable(); if(!file.exists() try FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(学生散列表); objectOut.close(); out.close(); catch(IOException e) operation.setLayout(new BorderLayout(); operation.add(ph, BorderLayout.NORTH); operation.add(pv, BorderLayout.CENTER); operation.add(plb, BorderLayout.SOUTH); operation.setDefaultCloseOperation(EXIT_ON_CLOSE); operation.setSize(700,500); operation.setVisible(false);(1).录入界面 InputAct类1). 成员变量主要成员变量描述变量类型变量名称获取输入的学号,姓名,专业,年级,出生,住址,电话Stringnumber,name,specialty,grade,borth,address,phonenumber,sex2) . 方法主要方法名称功能备注actionPerformed处理ActionEvent事件接口方法3) . 代码class InputAct implements ActionListener public void actionPerformed(ActionEvent e) 修改.setEnabled(false); String number="" number=学号.getText(); if(number.length()>0) try inOne=new FileInputStream(file); inTwo=new ObjectInputStream(inOne); 学生散列表=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); catch(Exception ee)System.out.println("创建散列表出现问题!"); if(学生散列表.containsKey(number) String warning="该生信息已存在,请到修改页面修改!" JOptionPane.showMessageDialog(null,warning,"警告", JOptionPane.WARNING_MESSAGE); else String m="该生信息将被录入!" int ok=JOptionPane.showConfirmDialog(null,m,"确认", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE); if(ok=JOptionPane.YES_OPTION) String name=姓名.getText(); String specialty=专业.getText(); String grade=年级.getText(); String borth=出生.getText(); String address=住址.getText(); String phonenumber=电话.getText(); String sex=null; if(男.isSelected()sex=男.getText(); elsesex=女.getText(); 学生=new Student(); 学生.setNumber(number); 学生.setName(name); 学生.setSpecialty(specialty); 学生.setGrade(grade); 学生.setBorth(borth); 学生.setSex(sex); try outOne=new FileOutputStream(file); outTwo=new ObjectOutputStream(outOne); 学生散列表.put(number,学生); outTwo.writeObject(学生散列表); outTwo.close(); outOne.close(); catch(Exception ee)System.out.println("输出散列表出现问题!"); 学号.setText(null); 姓名.setText(null); 专业.setText(null); 年级.setText(null); 出生.setText(null); else String warning="必须输入学号!" JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); (2).查询界面 InquestAct类1) . 成员变量主要成员变量描述变量属性变量名称获取输入的学号Stringnumber2). 方法 主要方法名称功能备注actionPerformed处理ActionEvent事件接口方法3).代码class InquestAct implements ActionListener public void actionPerformed(ActionEvent e) String number="" number=学号.getText(); if(number.length()>0) try inOne=new FileInputStream(file); inTwo=new ObjectInputStream(inOne); 学生散列表=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); catch(Exception ee)System.out.println("散列表有问题!"); if(学生散列表.containsKey(number) 修改.setEnabled(true); Student stu=(Student)学生散列表.get(number); 姓名.setText(stu.getName(); 专业.setText(stu.getSpecialty(); 年级.setText(stu.getGrade(); 出生.setText(stu.getBorth(); if(stu.getSex().equals("男")男.setSelected(true); else女.setSelected(true); else 修改.setEnabled(false); String warning="该学号不存在!" JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); else 修改.setEnabled(false); String warning="必须输入学号!" JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); 姓名.setEnabled(false); 男.setEnabled(false); 女.setEnabled(false); 专业.setEnabled(false); 年级.setEnabled(false); 出生.setEnabled(false); 电话.setEnabled(false); 住址.setEnabled(false); 确定.setVisible(true); 录入.setEnabled(false); 修改.setEnabled(false); 删除.setEnabled(false); 显示.setEnabled(false); (3).修改界面 ModifyAct类1). 成员变量主要成员变量描述变量类型变量名称获取修改的学号,姓名,专业,年级,出生,住址,电话Stringnumber,name,specialty,grade,borth,address,phonenumber,sex 2). 方法 主要方法名称功能备注actionPerformed处理ActionEvent事件接口方法3). 代码class ModifyAct implements ActionListener public void actionPerformed(ActionEvent e) String number=学号.getText(); String name=姓名.getText(); String specialty=专业.getText(); String grade=年级.getText(); String borth=出生.getText(); String phonenumber=电话.getText(); String address=住址.getText(); String sex=null; if(男.isSelected()sex=男.getText(); elsesex=女.getText(); String m="确定要保存修改该学生的记录吗?" int ok=JOptionPane.showConfirmDialog(null,m,"确认", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) Student 学生=new Student(); 学生.setNumber(number); 学生.setName(name); 学生.setSpecialty(specialty); 学生.setGrade(grade); 学生.setBorth(borth); 学生.setSex(sex); 学生.setAddress(address); 学生.setPhonenumber(phonenumber); try outOne=new FileOutputStream(file); outTwo=new ObjectOutputStream(outOne); 学生散列表.put(number, 学生); outTwo.writeObject(学生散列表); outTwo.close(); outOne.close(); 学号.setText(null); 姓名.setText(null); 专业.setText(null); 年级.setText(null); 出生.setText(null); 电话.setText(null); 住址.setText(null); catch(Exception ee) System.out.println("录入修改出现异常!"); 修改.setEnabled(false); (4).删除界面DeleteAct类 1). 成员变量主要成员变量描述变量属性变量名称获取输入的学号Stringnumber2). 方法 主要方法名称功能备注actionPerformed处理ActionEvent事件接口方法3).代码class DeleteAct implements ActionListener public void actionPerformed(ActionEvent e) 修改.setEnabled(false); String number=学号.getText(); if(number.length()>0) try inOne=new FileInputStream(file); inTwo=new ObjectInputStream(inOne); 学生散列表=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); catch(Exception ee) if(学生散列表.containsKey(number) Student stu=(Student)学生散列表.get(number); 姓名.setText(stu.getName(); 专业.setText(stu.getSpecialty(); 年级.setText(stu.getGrade(); 出生.setText(stu.getBorth(); 电话.setText(stu.getPhonenumber(); 住址.setText(stu.getAddress(); if(stu.getSex().equals("男")男.setSelected(true); else女.setSelected(true); String m="确定要删除该学生的记录吗?" int ok=JOptionPane.showConfirmDialog(null,m,"确认", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) 学生散列表.remove(number); try outOne=new FileOutputStream(file); outTwo=new ObjectOutputStream(outOne); outTwo.writeObject(学生散列表); outTwo.close(); outOne.close(); 学号.setText(null); 姓名.setText(null); 专业.setText(null); 年级.setText(null); 出生.setText(null); 电话.setText(null); 住址.setText(null); catch(Exception ee)System.out.println(ee); else if(ok=JOptionPane.NO_OPTION) 学号.setText(null); 姓名.setText(null); 专业.setText(null); 年级.setText(null); 出生.setText(null); 电话.setText(null); 住址.setText(null); else String warning="该学号不存在!" JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); else String warning="必须输入学号!" JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); (5).显示界面 ShowAct类 1). 成员变量主要成员变量属性描述变量属性变量名称存放“学生”对象的散列表Hashtable基本信息表显示学生基本信息的文本条JTextField学号,姓名,专业,年级,出生,住址,电话 2). 方法 主要方法名称功能备注ShowAct创建学生信息显示界面构造方法actionPerformed处理ActionEvent事件接口方法3). 代码class ShowAct implements ActionListener public void actionPerformed(ActionEvent e) new StudentShow(file); public class StudentShow extends JDialog Hashtable