📄 modifygrade.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class ModifyGrade extends JDialog implements ActionListener
{
Hashtable 基本成绩表=null;
StudentG 学生成=null;
JTextField 学号,语文,英语,数学;
JButton 开始修改,录入修改,重置;
JButton 退出;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file=null;
public ModifyGrade(JFrame f,File file)
{
super(f,"学生成绩修改",false);
this.file=file;
学号=new JTextField(10);
语文=new JTextField(10);
数学=new JTextField(10);
英语=new JTextField(10);
开始修改=new JButton("开始修改");
录入修改=new JButton("录入修改");
退出=new JButton("退出");
退出.addActionListener(this);
录入修改.setEnabled(false);
重置=new JButton("重置");
学号.addActionListener(this);
开始修改.addActionListener(this);
录入修改.addActionListener(this);
重置.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("输入要修改信息的学号:",JLabel.CENTER));
box1.add(学号);
box1.add(开始修改);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("(新)语文:",JLabel.CENTER));
box2.add(语文);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("(新)数学:",JLabel.CENTER));
box3.add(数学);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("(新)英语:",JLabel.CENTER));
box4.add(英语);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(录入修改);
pSouth.add(重置);
pSouth.add(退出);
add(pSouth,BorderLayout.SOUTH);
validate();
setBounds(100,200,360,270);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==开始修改||e.getSource()==学号)
{
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)
{
}
if(基本成绩表.containsKey(number))
{
录入修改.setEnabled(true);
StudentG stu=(StudentG)基本成绩表.get(number);
语文.setText(stu.getChinese());
数学.setText(stu.getMath());
英语.setText(stu.getEnglish());
}
else
{
录入修改.setEnabled(false);
String warning="该学号不存在!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
学号.setText(null);
语文.setText(null);
英语.setText(null);
数学.setText(null);
}
}
else
{
录入修改.setEnabled(false);
String warning="必须要输入学号!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
学号.setText(null);
语文.setText(null);
英语.setText(null);
数学.setText(null);
}
}
else if(e.getSource()==录入修改)
{
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)
{
}
if(基本成绩表.containsKey(number))
{
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 chinese=语文.getText();
String math=数学.getText();
String english=英语.getText();
学生成=new StudentG();
学生成.setNumber(number);
学生成.setChinese(chinese);
学生成.setMath(math);
学生成.setEnglish(english);
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);
}
catch(Exception ee)
{
System.out.println(ee);
}
录入修改.setEnabled(false);
}
else if(ok==JOptionPane.NO_OPTION)
{
录入修改.setEnabled(true);
}
}
else
{
String warning="该学号没有基本信息,不能修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
录入修改.setEnabled(false);
}
}
else
{
String warning="必须要输入学号!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
录入修改.setEnabled(false);
}
}
if(e.getSource()==重置)
{
学号.setText(null);
语文.setText(null);
数学.setText(null);
英语.setText(null);
}
if(e.getSource()==退出)
{
this.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -