📄 chengjichaxun.java
字号:
import java.util.*; //
import java.awt.event.*;
import java.awt.*;
import javax.swing.*; //swing包的调用
import java.io.*;
class Student extends JPanel //Student类,面板
{String 学号,姓名;float 分数1,分数2,平均分;
Student(String 学号,String 姓名,float 分数1,float 分数2,float 平均分) //构造方法Student
{this.学号=学号;
this.姓名=姓名;
this.分数1=分数1;
this.分数2=分数2;
平均分=分数1/2+分数2/2;
this.平均分=平均分;
}
}
class Show extends JFrame implements ActionListener //Show 类
{Hashtable hashtable=new Hashtable();
JTextField 学号文本框=new JTextField(),
姓名文本框=new JTextField(),
分数1文本框=new JTextField(),
分数2文本框=new JTextField(),
查询文本框=new JTextField();
JButton b_add=new JButton("添加成绩"),
b_show=new JButton("显示信息");
JTextField 学号显示条=new JTextField();
JTextField 成绩1显示条=new JTextField();
JTextField 成绩2显示条=new JTextField();
JTextField 平均分显示条=new JTextField();
JTextField 操作提示区=new JTextField("操作提示:"); //内部使用的组件定义
Show()
{Container con=getContentPane();
JPanel 成绩输入区=new JPanel(); //输入部分内部布局
成绩输入区.setLayout(new GridLayout(7,2));
成绩输入区.add(new Label("成绩输入区:"));
成绩输入区.add(new Label());
成绩输入区.add(new Label("学生学号:"));
成绩输入区.add(学号文本框);
成绩输入区.add(new Label("学生姓名:"));
成绩输入区.add(姓名文本框);
成绩输入区.add(new Label("学生成绩1:"));
成绩输入区.add(分数1文本框);
成绩输入区.add(new Label("学生成绩2:"));
成绩输入区.add(分数2文本框);
成绩输入区.add(new Label("点击添加:"));
成绩输入区.add(b_add);
成绩输入区.add(new Label());
成绩输入区.add(new Label());
JPanel 查询显示区=new JPanel(); //查询部分内部布局
查询显示区.setLayout(new GridLayout(7,2));
查询显示区.add(new Label("成绩查询区:"));
查询显示区.add(new Label());
查询显示区.add(new Label("输入学生的姓名:"));
查询显示区.add(查询文本框);
查询显示区.add(new Label("点击查询:"));
查询显示区.add(b_show);
查询显示区.add(new Label("学号:"));
查询显示区.add(学号显示条);
查询显示区.add(new Label("成绩1:"));
查询显示区.add(成绩1显示条);
查询显示区.add(new Label("成绩2:"));
查询显示区.add(成绩2显示条);
查询显示区.add(new Label("平均分:"));
查询显示区.add(平均分显示条);
con.add(成绩输入区,BorderLayout.WEST); //整体布局
con.add(查询显示区,BorderLayout.EAST);
con.add(操作提示区,BorderLayout.SOUTH);
con.add(new Label("学生成绩输入与查询系统"),BorderLayout.NORTH);
setBounds(300,250,330,280);
pack();
b_add.addActionListener(this); //监视器
b_show.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==b_add)
{String 学号=null,姓名=null;float 分数1=0.0f;float 分数2=0.0f;float 平均分=0.0f;
boolean y=true;
学号=学号文本框.getText().trim();
姓名=姓名文本框.getText().trim();
if((学号).equals("")||(姓名).equals(""))
{y=false;
操作提示区.setText("操作提示:请输入正确的姓名和学号");
}
else{try{分数1=Float.valueOf(分数1文本框.getText()).floatValue();
分数2=Float.valueOf(分数2文本框.getText()).floatValue();
}
catch(NumberFormatException ee)
{y=false;
操作提示区.setText("操作提示:请输入正确的成绩");
}
}
if(y==true)
{操作提示区.setText("操作提示:");
学号文本框.setText(null);
姓名文本框.setText(null);
分数1文本框.setText(null);
分数2文本框.setText(null);
Student stu=new Student(学号,姓名,分数1,分数2,平均分);
hashtable.put(姓名,stu);
try {FileOutputStream file=new FileOutputStream("score.txt"); //正确时由文本写入
ObjectOutputStream out=new ObjectOutputStream(file);
out.writeObject(hashtable);
out.close();
}
catch(IOException event){}
}
}
else if(e.getSource()==b_show)
{String temp=null;
temp=查询文本框.getText();
学号显示条.setText(null);
成绩1显示条.setText(null);
成绩2显示条.setText(null);
平均分显示条.setText(null);
try{FileInputStream come_in=new FileInputStream("score.txt"); //由文本读出
ObjectInputStream in=new ObjectInputStream(come_in);
hashtable=(Hashtable)in.readObject();
in.close();
}
catch(ClassNotFoundException event)
{}
catch(IOException event)
{操作提示区.setText("操作提示:文件无法读出");}
if(hashtable.get(temp)!=null)
{Student s=(Student)hashtable.get(temp);
学号显示条.setText(" "+s.学号);
成绩1显示条.setText(" "+s.分数1);
成绩2显示条.setText(" "+s.分数2);
平均分显示条.setText(" "+s.平均分);
操作提示区.setText("操作提示:");
}
else{操作提示区.setText("操作提示:不存在此人信息,请确定是否输入");}
}
}
}
public class HH //HH 类
{public static void main(String args[])
{Show win=new Show();
win.setVisible(true);
win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -