📄 modifysituation.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class ModifySituation extends JPanel implements ActionListener{
Hashtable baseInformation=null;
JTextField number,name,math,english,computer;
JButton preModify,saveModify,reset;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file=null;
public ModifySituation(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);
preModify=new JButton("开始修改");
saveModify=new JButton("录入修改");
saveModify.setEnabled(false);
reset=new JButton("重置");
number.addActionListener(this);
preModify.addActionListener(this);
saveModify.addActionListener(this);
reset.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("输入要修改信息的学号:",JLabel.CENTER));
box1.add(number);
box1.add(preModify);
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(saveModify);
pSouth.add(reset);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==preModify||e.getSource()==number){
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)){
saveModify.setEnabled(true);
Student stu=(Student)baseInformation.get(stunumber);
name.setText(stu.getName());
math.setText(stu.getMath());
english.setText(stu.getEnglish());
computer.setText(stu.getComputer());
}
else{
saveModify.setEnabled(false);
String warning="该学号不存在!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
number.setText(null);
name.setText(null);
math.setText(null);
english.setText(null);
computer.setText(null);
}
}
else{
saveModify.setEnabled(false);
String warning="必须要输入学号!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
number.setText(null);
name.setText(null);
math.setText(null);
english.setText(null);
computer.setText(null);
}
}
else if(e.getSource()==saveModify){
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 question="该生基本信息已存在,您想修改他(她)的基本信息吗?";
JOptionPane.showMessageDialog(this,question,"警告",JOptionPane.QUESTION_MESSAGE);
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 stu=new Student();
stu.setNumber(stunumber);
stu.setName(stuname);
stu.setMath(stumath);
stu.setEnglish(stuenglish);
stu.setComputer(stucomputer);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
baseInformation.put(stunumber,stu);
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);
}
saveModify.setEnabled(false);
}
else if(ok==JOptionPane.NO_OPTION){
saveModify.setEnabled(true);
}
}
else{
String warning="该学号没有基本信息,不能修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
saveModify.setEnabled(false);
}
}
else{
String warning="必须要输入学号!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
saveModify.setEnabled(false);
}
}
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 + -