📄 studentsituation.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class StudentSituation extends JPanel implements ActionListener{
Hashtable baseInformation=null;
JTextField number,name,math,english,computer;
Student student=null;
JButton save,reset;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file=null;
public StudentSituation(File file){
this.file=file;
number=new JTextField(10);
name=new JTextField(10);
math=new JTextField(10);
english=new JTextField(10);
computer=new JTextField(10);
save=new JButton("录入");
reset=new JButton("重置");
save.addActionListener(this);
reset.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("学号:",JLabel.CENTER));
box1.add(number);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("姓名:",JLabel.CENTER));
box2.add(name);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("在下面输入各门课程成绩(0-100的整数)",JLabel.CENTER));
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("数学成绩:",JLabel.CENTER));
box4.add(math);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("英语成绩:",JLabel.CENTER));
box5.add(english);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("电脑成绩:",JLabel.CENTER));
box6.add(computer);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(save);
pSouth.add(reset);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==save){
String stunumber="";
stunumber=number.getText();
if(stunumber.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
baseInformation=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){
}
if(baseInformation.containsKey(stunumber)){
String warning="该生基本信息已存在,请到修改页面修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else{
String m="基本信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
String stuname=name.getText();
String stumath=math.getText();
String stuenglish=english.getText();
String stucomputer=computer.getText();
student=new Student();
student.setNumber(stunumber);
student.setName(stuname);
student.setMath(stumath);
student.setEnglish(stuenglish);
student.setComputer(stucomputer);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
baseInformation.put(stunumber,student);
outTwo.writeObject(baseInformation);
outTwo.close();
outOne.close();
number.setText(null);
name.setText(null);
math.setText(null);
english.setText(null);
computer.setText(null);
}
catch(Exception ee){
System.out.println(ee);
}
}
}
}
else{
String warning="必须要输入学号!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==reset){
number.setText(null);
name.setText(null);
math.setText(null);
english.setText(null);
computer.setText(null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -