题目要求:
使用字符流和和GUI类 编程实现以下功能:
(1)设计图形化界面,至少包括文本类控件类。接收从键盘输入姓名、学号、成绩,并保存到文本文件中,重复进行。
(2)从文件中读取各学生的成绩,并计算所有学生成绩的平均值、最大值和最小值,排序后输出到另一文本文件。
思路:设计三个类来完成:StudentGUI类、StudentListen类、StudentInformation类
(代码的大部分关键步骤都已在旁边标明了详细注释)
GUI类(实现GUI界面)
package homework0405;
import javax.swing.*;
import java.awt.*;
public class StudentGUI {
public static void main(String[] args) {
// 实例化
new StudentGUI().init_1();
}
// 主界面
public void init_1() {
// 定义容器
JFrame MyJFrame = new JFrame("学生信息管理系统");
// 定义面板
JPanel MyJpanel[] = new JPanel[5];
// 窗口大小
MyJFrame.setBounds(150,150,400, 220);
// 窗口布局
MyJFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
// 窗口不可调整
MyJFrame.setResizable(false);
// 定义字体
Font font=new Font("黑体",Font.PLAIN,15);
// 添加标题
JLabel MyJlabel = new JLabel("添 加 学 生 信 息");
MyJlabel.setFont(font);
MyJpanel[0] = new JPanel();
MyJpanel[0].add(MyJlabel);
MyJFrame.add(MyJpanel[0]);
String s[] = new String[3];
s[0] = " 学 号:";
s[1] = " 姓 名:";
s[2] = " 成 绩:";
// 标签
JLabel jlabel[] = new JLabel[3];
// 文本框
JTextField jtextfield[] = new JTextField[3];
// 实例化
for (int i = 0; i < 3; i++) {
MyJpanel[i+1] = new JPanel();
jlabel[i] = new JLabel(s[i]);
MyJpanel[i+1].add(jlabel[i]);
jtextfield[i] = new JTextField(30);
MyJpanel[i+1].add(jtextfield[i]);
}
// 按钮
JButton jbutton_1 = new JButton("添加");
JButton jbutton_2 = new JButton("保存");
JButton jbutton_3 = new JButton("排序");
// 加入按钮
MyJpanel[4] = new JPanel();
MyJpanel[4].add(jbutton_1);
MyJpanel[4].add(jbutton_2);
MyJpanel[4].add(jbutton_3);
// 加入组件
for (int i = 0; i < 5; i++){
MyJFrame.add(MyJpanel[i]);
}
// 窗口显示
MyJFrame.setVisible(true);
// 关闭窗口则退出程序
MyJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 注册监听
StudentListen e = new StudentListen(jbutton_1, jbutton_2, jbutton_3, jtextfield);
jbutton_1.addActionListener(e);
jbutton_2.addActionListener(e);
jbutton_3.addActionListener(e);
}
}
StudentInformation类(来存储所有学生的信息)
package homework0405;
import java.util.ArrayList;
//定义学生类
public class StudentInformation {
// 存储学生总人数
public static ArrayList<StudentInformation> all_student=new ArrayList<>();
// 存储排序后的数据
public static ArrayList<StudentInformation> sort_student=new ArrayList<>();
private String ID;//学号
private String Name;//姓名
private double Score;//成绩
//用于实例化
public StudentInformation() {
}
public StudentInformation(String ID,String Name,double Score) {
this.ID=ID;
this.Name=Name;
this.Score=Score;
}
//获得数据
public String get_ID() {
return ID;
}
public String get_Name() {
return Name;
}
public double get_Score() {
return Score;
}
}
StudentListen类(鼠标监听,实现人机交互)
package homework0405;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
// 创建鼠标监听类
public class StudentListen implements ActionListener {
JButton jbutton_1;
JButton jbutton_2;
JButton jbutton_3;
JTextField jtextfield[];
public StudentListen(JButton jbutton_1, JButton jbutton_2, JButton jbutton_3, JTextField[] jtextfield) { //构造函数,传入监听信息
this.jbutton_1=jbutton_1;
this.jbutton_2=jbutton_2;
this.jbutton_3=jbutton_3;
this.jtextfield=jtextfield;
}
// 创建两个存入的文件
File StudentFile=new File("D:\\Homework4\\homework4\\src\\homework0405\\Student.txt");
File SortFile=new File("D:\\Homework4\\homework-4\\src\\homework0405\\Sort.txt");
@Override
public void actionPerformed(ActionEvent e) {
// 点击了提交按钮
if(e.getSource()==jbutton_1) {
// 判断标志
boolean mark = false;
// 判断学号是否重复
int k=0;
for(k = 0;k < StudentInformation.all_student.size(); k++) {
if(jtextfield[0].getText().equals(StudentInformation.all_student.get(k).get_ID())) {
// 学号重复
mark = false;
// 提示
JOptionPane.showMessageDialog(jbutton_1, "学号重复!\n添加学生失败!","消息提示",JOptionPane.WARNING_MESSAGE);
break;
}
}
if(k == StudentInformation.all_student.size()) {
mark = true;
}
// mark=true则添加学生信息
if(mark) {
StudentInformation student=new StudentInformation(jtextfield[0].getText(), jtextfield[1].getText(), Double.parseDouble(jtextfield[2].getText()));
// 将学生对象加入总对象中
StudentInformation.all_student.add(student);
JOptionPane.showMessageDialog(jbutton_1, "添加学生成功!!!", "提示",JOptionPane.PLAIN_MESSAGE);
}
for(int i = 0; i < jtextfield.length; i++) {
jtextfield[i].setText("");
}
}
// 点了保存按钮
else if(e.getSource()==jbutton_2){
//建立输出
FileOutputStream out=null;
try {
out =new FileOutputStream(StudentFile);
String student;
for(int i=0; i < StudentInformation.all_student.size(); i++) {
//创建缓冲区,写入数据
student =StudentInformation.all_student.get(i).get_ID()+" ";
byte buffer[] =student.getBytes();
out.write(buffer);
student =StudentInformation.all_student.get(i).get_Name()+" ";
byte buffer1[] = student.getBytes();
out.write(buffer1);
student =StudentInformation.all_student.get(i).get_Score()+" ";
byte buffer2[] = student.getBytes();
out.write(buffer2);
//换行
out.write('\n');
}
JOptionPane.showMessageDialog(null, "保存成功!", "提示",JOptionPane.PLAIN_MESSAGE);
}catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
if(out!=null) {
out.close();
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
// 点了排序按钮
else if(e.getSource()==jbutton_3){
BufferedReader br= null;//打开一个输入流,然后准备读入数据
try {
br = new BufferedReader(new FileReader(StudentFile));
String line=null; //读入的每行
while((line=br.readLine())!=null){ //如果不是什么都没有就进行处理
String[] elements=line.split(" "); //使用split函数对于读到的文本进行处理
StudentInformation student=new StudentInformation(elements[0],elements[1],Double.parseDouble(elements[2]));//构造学生,添加学生信息
StudentInformation.sort_student.add(student);//添加学生信息
}
br.close();//关闭读入流
} catch (IOException ex) {
ex.printStackTrace();
}
// 冒泡排序
StudentInformation Student;
for(int i = 0; i<StudentInformation.sort_student.size()-1; i++) {
for(int j = 0; j<StudentInformation.sort_student.size()-1; j++) {
if(StudentInformation.sort_student.get(j).get_Score() < StudentInformation.sort_student.get(j+1).get_Score()) {
Student = StudentInformation.sort_student.get(j);
StudentInformation.sort_student.set(j, StudentInformation.sort_student.get(j+1));
StudentInformation.sort_student.set(j+1, Student);
}
}
}
// 找出最大值 最小值 并计算平均值
double average, sum =0.0 ;
double max = StudentInformation.sort_student.get(0).get_Score(),
min = StudentInformation.sort_student.get(0).get_Score();
for(int i = 0; i < StudentInformation.sort_student.size(); i++) {
sum += StudentInformation.sort_student.get(i).get_Score();
if(StudentInformation.sort_student.get(i).get_Score() > max){
max = StudentInformation.sort_student.get(i).get_Score();
}
if(StudentInformation.sort_student.get(i).get_Score() < min){
min = StudentInformation.sort_student.get(i).get_Score();
}
}
average = sum / StudentInformation.sort_student.size();
// 把排序好的数据存入新文件
FileOutputStream Out = null;
try {
Out =new FileOutputStream(SortFile);
String student;
for(int i=0; i < StudentInformation.sort_student.size(); i++) {
//创建缓冲区,写入数据
student =StudentInformation.sort_student.get(i).get_ID()+" ";
byte buffer[] =student.getBytes();
Out.write(buffer);
student =StudentInformation.sort_student.get(i).get_Name()+" ";
byte buffer1[] = student.getBytes();
Out.write(buffer1);
student =StudentInformation.sort_student.get(i).get_Score()+" ";
byte buffer2[] = student.getBytes();
Out.write(buffer2);
//换行
Out.write('\n');
}
//换行
Out.write('\n');
Out.write('\n');
String explain = "平均分 :" + average + " " + "最大值 : " + max + " " + "最小值 : " + min;
byte buf[] =explain.getBytes();
Out.write(buf);
JOptionPane.showMessageDialog(null,"排序成功!","提示",JOptionPane.PLAIN_MESSAGE);
}catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
if(Out!=null) {
Out.close();
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
}
}
程序运行情况:
后期拓展:这里只是实现了学生信息的添加,可在此基础上添加方法来实现更多的功能,比如学生信息的查找,删除和修改等等,GUI界面的丰富和美化都值得去研究!