java学生学籍管理系统培训资料.docx
《java学生学籍管理系统培训资料.docx》由会员分享,可在线阅读,更多相关《java学生学籍管理系统培训资料.docx(27页珍藏版)》请在冰豆网上搜索。
java学生学籍管理系统培训资料
java学生学籍管理系统
主类;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.io.*;
importjava.util.Hashtable;
publicclassStudentManagerextendsJFrameimplementsActionListener{
StudentSituation基本信息录入=null;
ModifySituation基本信息修改=null;
Inquest基本信息查询=null;
Delete基本信息删除=null;
JMenuBarbar;
JMenufileMenu;
JMenuItem录入,修改,查询,删除;
Containercon=null;
Hashtable基本信息=null;//存放基本信息的散列表
Filefile=null;//存放散列表的文件
CardLayoutcard=null;//卡片布局
JLabellabel=null;
JPanelpCenter;
publicStudentManager(){
录入=newJMenuItem("录入学生基本信息");
修改=newJMenuItem("修改学生基本信息");
查询=newJMenuItem("查询学生基本信息");
删除=newJMenuItem("删除学生基本信息");
bar=newJMenuBar();
fileMenu=newJMenu("菜单选项");
fileMenu.add(录入);
fileMenu.add(修改);
fileMenu.add(查询);
fileMenu.add(删除);
bar.add(fileMenu);
setJMenuBar(bar);
label=newJLabel("欢迎使用学生基本信息管理系统",JLabel.CENTER);
label.setFont(newFont("TimesRoman",Font.BOLD,24));
label.setForeground(Color.red);
基本信息=newHashtable();
录入.addActionListener(this);
修改.addActionListener(this);
查询.addActionListener(this);
删除.addActionListener(this);
card=newCardLayout();
con=getContentPane();
pCenter=newJPanel();
pCenter.setLayout(card);
file=newFile("基本信息.txt");
if(!
file.exists()){
try{
FileOutputStreamout=newFileOutputStream(file);
ObjectOutputStreamobjectOut=newObjectOutputStream(out);
objectOut.writeObject(基本信息);
objectOut.close();
out.close();
}
catch(IOExceptione){
}
}
基本信息录入=newStudentSituation(file);
基本信息修改=newModifySituation(file);
JFramef=newJFrame();
基本信息查询=newInquest(f,file);
基本信息删除=newDelete(file);
pCenter.add("欢迎语界面",label);
pCenter.add("录入界面",基本信息录入);
pCenter.add("修改界面",基本信息修改);
pCenter.add("删除界面",基本信息删除);
con.add(pCenter,BorderLayout.CENTER);
con.validate();
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,420,380);
validate();
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==录入){
card.show(pCenter,"录入界面");
}
elseif(e.getSource()==修改){
card.show(pCenter,"修改界面");
}
elseif(e.getSource()==查询){
基本信息查询.setVisible(true);//基本信息查询查询窗口设置为独立,便于用户查询
}
elseif(e.getSource()==删除){
card.show(pCenter,"删除界面");
}
}
publicstaticvoidmain(Stringargs[]){
newStudentManager();
}
}
录入界面:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.io.*;
importjava.util.*;
@SuppressWarnings("unchecked")publicclassStudentSituationextendsJPanelimplementsActionListener{
Hashtable基本信息表=null;//存放学生基本信息的散列表
JTextField学号,姓名,专业,年级,出生;
JRadioButton男,女;
Student学生=null;
ButtonGroupgroup=null;
JButton录入,重置;
FileInputStreaminOne=null;
ObjectInputStreaminTwo=null;
FileOutputStreamoutOne=null;
ObjectOutputStreamoutTwo=null;
Filefile=null;
publicStudentSituation(Filefile){
this.file=file;
学号=newJTextField(10);
姓名=newJTextField(10);
专业=newJTextField(10);
年级=newJTextField(10);
出生=newJTextField(10);
group=newButtonGroup();
男=newJRadioButton("男",true);
女=newJRadioButton("女",false);
group.add(男);
group.add(女);
录入=newJButton("录入");
重置=newJButton("重置");
录入.addActionListener(this);
重置.addActionListener(this);
Boxbox1=Box.createHorizontalBox();
box1.add(newJLabel("学号:
",JLabel.CENTER));
box1.add(学号);
Boxbox2=Box.createHorizontalBox();
box2.add(newJLabel("姓名:
",JLabel.CENTER));
box2.add(姓名);
Boxbox3=Box.createHorizontalBox();
box3.add(newJLabel("性别:
",JLabel.CENTER));
box3.add(男);
box3.add(女);
Boxbox4=Box.createHorizontalBox();
box4.add(newJLabel("专业:
",JLabel.CENTER));
box4.add(专业);
Boxbox5=Box.createHorizontalBox();
box5.add(newJLabel("年级:
",JLabel.CENTER));
box5.add(年级);
Boxbox6=Box.createHorizontalBox();
box6.add(newJLabel("出生:
",JLabel.CENTER));
box6.add(出生);
BoxboxH=Box.createVerticalBox();//创建一个垂直的box容器,其中从上向下排列
//6个水平box容器
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());//垂直box容器内的组件靠上对齐
JPanelpCenter=newJPanel();
pCenter.add(boxH);
setLayout(newBorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanelpSouth=newJPanel();
pSouth.add(录入);
pSouth.add(重置);
add(pSouth,BorderLayout.SOUTH);
validate();
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==录入){
Stringnumber="";
number=学号.getText();
if(number.length()>0){
try{
inOne=newFileInputStream(file);
inTwo=newObjectInputStream(inOne);
基本信息表=(Hashtable)inTwo.readObject();//首先到文件中读取hashtable对象
inOne.close();
inTwo.close();
}
catch(Exceptionee){
}
if(基本信息表.containsKey(number)){//如果该生学号对应的基本信息已经存在,弹出对话框
Stringwarning="该生基本信息已存在,请到修改页面修改!
";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else{
Stringm="基本信息将录入";
intok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
Stringname=姓名.getText();
Stringdiscipling=专业.getText();
Stringgrade=年级.getText();
Stringborth=出生.getText();
Stringsex=null;
if(男.isSelected()){
sex=男.getText();
}
else{
sex=女.getText();
}
学生=newStudent();
学生.setNumber(number);
学生.setName(name);
学生.setDiscipling(discipling);
学生.setGrade(grade);
学生.setBorth(borth);
学生.setSex(sex);
try{
outOne=newFileOutputStream(file);
outTwo=newObjectOutputStream(outOne);
基本信息表.put(number,学生);
outTwo.writeObject(基本信息表);
outTwo.close();
outOne.close();
学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
catch(Exceptionee){
System.out.println(ee);
}
}
}
}
else{
Stringwarning="必须输入学号";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==重置){
学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
}
}
查询界面:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.io.*;
importjava.util.*;
publicclassInquestextendsJDialogimplementsActionListener{
Hashtable基本信息表=null;
JTextField学号,姓名,专业,年级,出生;
JRadioButton男,女;
JButton查询;
ButtonGroupgroup=null;
FileInputStreaminOne=null;
ObjectInputStreaminTwo=null;
Filefile=null;
publicInquest(JFramef,Filefile){
super(f,"查询对话框",false);
this.file=file;
学号=newJTextField(11);
查询=newJButton("查询");
学号.addActionListener(this);
查询.addActionListener(this);
姓名=newJTextField(10);
姓名.setEditable(false);
专业=newJTextField(10);
专业.setEditable(false);
年级=newJTextField(10);
年级.setEditable(false);
出生=newJTextField(10);
出生.setEditable(false);
男=newJRadioButton("男",false);
女=newJRadioButton("女",false);
group=newButtonGroup();
group.add(男);
group.add(女);
Boxbox1=Box.createHorizontalBox();//以下创建6个水平box容器,容器内从左向右排列组件。
box1.add(newJLabel("输入要查询的学号:
",JLabel.CENTER));
box1.add(学号);
box1.add(查询);
Boxbox2=Box.createHorizontalBox();
box2.add(newJLabel("姓名:
",JLabel.CENTER));
box2.add(姓名);
Boxbox3=Box.createHorizontalBox();
box3.add(newJLabel("性别:
",JLabel.CENTER));
box3.add(男);
box3.add(女);
Boxbox4=Box.createHorizontalBox();
box4.add(newJLabel("专业;",JLabel.CENTER));
box4.add(专业);
Boxbox5=Box.createHorizontalBox();
box5.add(newJLabel("年级:
",JLabel.CENTER));
box5.add(年级);
Boxbox6=Box.createHorizontalBox();
box6.add(newJLabel("出生:
",JLabel.CENTER));
box6.add(出生);
BoxboxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());//使垂直的Box容器内的组件靠上对齐。
JPanelpCenter=newJPanel();
pCenter.add(boxH);
Containercon=getContentPane();
con.add(pCenter,BorderLayout.CENTER);
con.validate();
setVisible(false);
setBounds(100,200,360,270);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
setVisible(false);
}
});
}
publicvoidactionPerformed(ActionEvente){
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
if(e.getSource()==查询||e.getSource()==学号){
Stringnumber="";
number=学号.getText();
if(number.length()>0){
try{
inOne=newFileInputStream(file);
inTwo=newObjectInputStream(inOne);
基本信息表=(Hashtable)inTwo.readObject();//首先从文件中读取hashtable对象
inOne.close();
inTwo.close();
}
catch(Exceptionee){
}
if(基本信息表.containsKey(number)){
Studentstu=(Student)基本信息表.get(number);
姓名.setText(stu.getName());
专业.setText(stu.getDiscipling());
年级.setText(stu.getGrade());
出生.setText(stu.getBorth());
if(stu.getSex().equals("男")){
男.setSelected(true);
}
else{
女.setSelected(true);
}
}
else{
Stringwarning="该学号不存在!
";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
else{
Stringwarning="必须输入学号!
";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
}
修改界面:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.io.*;
importja