码迷,mamicode.com
首页 > 编程语言 > 详细

学生成绩管理系统(java swing)

时间:2021-01-07 12:18:50      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:frame   analysis   rgs   panel   name   display   exception   select   计算   

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.filechooser.FileNameExtensionFilter;

class Interface {
    JFrame jf = new JFrame("From:shish");
    JPanel jp1 = new JPanel(), jp2 = new JPanel(), jpinit = new JPanel(), jpAbout = new JPanel();
    JPanel jptitle = new JPanel();
    Box vbox1 = Box.createVerticalBox(); // 底下编辑垂直分布
    Box vboxmain = Box.createVerticalBox(); // 主要展示面垂直分布
    Menu m = new Menu(); // 菜单
    editBottom eb = new editBottom(); // 底部编辑框
    Data d = new Data(); // 数据处理
    File nfile = null; // 保存地址
    File rfile = null; // 读取文件地址
    public int now_number = 0; // 系统中学生数量
    public int now_sum = 0;  // 是否展示总分
    public int now_avg = 0; // 是否展示平均分
    public int new_file = 0; // 是否存在新建的页面
    public int fenxi = 0; // 是否展示数据分析
    public int now_view = 0; // 是否为视图状态
    public int now_save = 0; // 是否保存

    // 主页
    public void initInterface() {
        vboxmain.removeAll();
        JTextArea jta = new JTextArea();
        jta.setBackground(Color.white);
        jta.setText("\n\n\n\n                                                  " +
                " 欢 迎 \n                         " +
                "           使 用 学 生 管 理 系 统 !\n\n\n         " +
                "                   独学而无友┗( ▔, ▔ )┛则孤陋寡闻             " +
                "              \n\t\t ?ゝ??)ノ?\n\n\n\n\n\n\n");
        jta.setFont(new Font("华文行楷", Font.PLAIN, 34));
        jta.setEditable(false);
        jpinit.add(jta, "Center");
        vboxmain.add(jpinit);
        vboxmain.repaint();
        jf.add(vboxmain);
    }
    // 菜单关于界面
    public void aboutInterface() {
        JTextArea jta = new JTextArea();
        jta.setBackground(Color.white);
        jta.setText("\n\n作者:shish                                     " +
                "                                                      " +
                "\n博客:https://www.cnblogs.com/shish\n" +
                "GitHub:https://github.com/shishenhao\n" +
                "关于作者:超级无敌的菜鸡\n" +
                "声援作者:您的鼓励是作者的最大动力!\n\n\n\n\n\n\n\n\n\n\n");
        jta.setFont(new Font("华文行楷", Font.PLAIN, 32));
        jta.setEditable(false);
        jpAbout.add(jta, "Center");
        vboxmain.add(jpAbout);
        jf.add(vboxmain);
    }
    // 新建一个窗口的界面
    public void newCreate() {
        jptitle.removeAll();
        JTextArea jta = new JTextArea(1, 70);
        jta.setBackground(Color.white);
        jta.setEditable(false);
        jta.setFont(new Font("华文行楷", Font.PLAIN, 28));
        jta.setText("                               \t\t               学 生 成 绩 管 理 系 统  ");
        JTextArea jtaa = new JTextArea(1, 70);
        jtaa.setBackground(Color.LIGHT_GRAY);
        jtaa.setFont(new Font("微软雅黑", Font.PLAIN, 22));
        jtaa.setEditable(false);
        jtaa.setText("\t                 编号         |        学号         |        姓名        |       课程1        |        课程2        |        总分          ");
        vboxmain.removeAll();
        jptitle.add(jta);
        jptitle.add(jtaa);
        vboxmain.add(jptitle);
        vboxmain.repaint();
        jf.setVisible(true);
    }
    // 展现数据
    public void showData() {
        JTextArea[] jtas = new JTextArea[20];
        vboxmain.removeAll();
        jptitle.removeAll();
        JTextArea jta = new JTextArea(1, 70);
        jta.setBackground(Color.white);
        jta.setEditable(false);
        jta.setFont(new Font("华文行楷", Font.PLAIN, 28));
        jta.setText("                               \t\t               学 生 成 绩 管 理 系 统  ");
        JTextArea jtaa = new JTextArea(1, 70);
        jtaa.setBackground(Color.LIGHT_GRAY);
        jtaa.setFont(new Font("微软雅黑", Font.PLAIN, 22));
        jtaa.setEditable(false);
        jtaa.setText("\t                 编号         |        学号         |        姓名        |       课程1        |        课程2        |        总分          ");
        jptitle.add(jta);
        jptitle.add(jtaa);
        if (now_number <= 0) {
            vboxmain.add(jptitle);
            vboxmain.repaint();
            jf.setVisible(true);
            return;
        }
        for (int i = 1; i <= now_number; i++) {
            jtas[i] = new JTextArea();
            jtas[i].setBackground(Color.white);
            if (now_sum == 0) {
                jtas[i].setText(String.format("                     %d                  %s              %s%25.1f%25.1f    " +
                        "                                              ", i, d.zhuyao[i].id, d.zhuyao[i].name, d.zhuyao[i].grade[1], d.zhuyao[i].grade[2]));
            } else if (now_sum == 1) {
                jtas[i].setText(String.format("                     %d                  %s              %s%25.1f%25.1f%24.2f                  " +
                        "    ", i, d.zhuyao[i].id, d.zhuyao[i].name, d.zhuyao[i].grade[1], d.zhuyao[i].grade[2], d.zhuyao[i].grade[3]));
            }
            jtas[i].setFont(new Font("华文行楷", Font.PLAIN, 20));
            jtas[i].setEditable(false);
            jptitle.add(jtas[i]);
        }
        if (now_avg == 1) {
            JTextArea jav = new JTextArea();
            jav.setBackground(Color.white);
            jav.setFont(new Font("华文行楷", Font.PLAIN, 20));
            jav.setEditable(false);
            if (now_sum == 1) {
                jav.setText(String.format("               平均分                           " +
                        "                             %25.2f%25.2f%23.2f    " +
                        "                ", d.avg[1], d.avg[2], d.avg[3]));
            } else {
                jav.setText(String.format("                  平均分                                                         %25.2f%25.2f    " +
                        "                                              ", d.avg[1], d.avg[2]));
            }
            jptitle.add(jav);
        }
        jptitle.add(createEmptyJTA());
        if (fenxi == 1) {
            JTextArea jfx = new JTextArea();
            jfx.setBackground(Color.white);
            jfx.setFont(new Font("华文行楷", Font.PLAIN, 20));
            jfx.setEditable(false);
            d.analysis();
            jfx.setText(String.format(" 课程1成绩分析:优秀人数(>=85)为 %d 占比 %.2f,  " +
                            "良好人数(>=70)为 %d 占比 %.2f;                " +
                            "                              \n                      " +
                            "    及格人数(>=60)为 %d 占比 %.2f,  不及格人数(<60)为 %d " +
                            "占比 %.2f。", d.numYouxiu[0], d.youxiuB[0], d.numLianghao[0], d.lianghaoB[0],
                    d.numJige[0], d.jigeB[0], d.numBujige[0], d.bujigeB[0]));
            jfx.append(String.format("\n 课程2成绩分析:优秀人数(>=85)为 %d 占比 %.2f,  " +
                            "良好人数(>=70)为 %d 占比 %.2f;                " +
                            "                              \n                      " +
                            "    及格人数(>=60)为 %d 占比 %.2f,  不及格人数(<60)为 %d " +
                            "占比 %.2f。", d.numYouxiu[1], d.youxiuB[1], d.numLianghao[1], d.lianghaoB[1],
                    d.numJige[1], d.jigeB[1], d.numBujige[1], d.bujigeB[1]));
            jptitle.add(jfx);
            jptitle.add(createEmptyJTA());
        }
        vboxmain.add(jptitle);
        vboxmain.repaint();
        jf.setVisible(true);
    }
    // 展现视图
    public void showView(){
        JTextArea[] jtas = new JTextArea[20];
        vboxmain.removeAll();
        jptitle.removeAll();
        JTextArea jta = new JTextArea(1, 70);
        jta.setBackground(Color.white);
        jta.setEditable(false);
        jta.setFont(new Font("华文行楷", Font.PLAIN, 28));
        jta.setText("                               \t\t               学 生 成 绩 管 理 系 统  ");
        JTextArea jtaa = new JTextArea(1, 70);
        jtaa.setBackground(Color.LIGHT_GRAY);
        jtaa.setFont(new Font("微软雅黑", Font.PLAIN, 22));
        jtaa.setEditable(false);
        jtaa.setText("\t                 编号         |        学号         |        姓名        |       课程1        |        课程2        |        总分          ");
        jptitle.add(jta);
        jptitle.add(jtaa);
        if (now_number <= 0) {
            vboxmain.add(jptitle);
            vboxmain.repaint();
            jf.setVisible(true);
            return;
        }
        for (int i = 1; i <= now_number; i++) {
            jtas[i] = new JTextArea();
            jtas[i].setBackground(Color.white);
            if (now_sum == 0) {
                jtas[i].setText(String.format("                     %d                  %s              %s%25.1f%25.1f    " +
                        "                                              ", i, d.vDate[i].id, d.vDate[i].name, d.vDate[i].grade[1], d.vDate[i].grade[2]));
            } else if (now_sum == 1) {
                jtas[i].setText(String.format("                     %d                  %s              %s%25.1f%25.1f%24.2f                  " +
                        "    ", i, d.vDate[i].id, d.vDate[i].name, d.vDate[i].grade[1], d.vDate[i].grade[2], d.vDate[i].grade[3]));
            }
            jtas[i].setFont(new Font("华文行楷", Font.PLAIN, 20));
            jtas[i].setEditable(false);
            jptitle.add(jtas[i]);
        }
        if (now_avg == 1) {
            JTextArea jav = new JTextArea();
            jav.setBackground(Color.white);
            jav.setFont(new Font("华文行楷", Font.PLAIN, 20));
            jav.setEditable(false);
            if (now_sum == 1) {
                jav.setText(String.format("               平均分                           " +
                        "                             %25.2f%25.2f%23.2f    " +
                        "                ", d.avg[1], d.avg[2], d.avg[3]));
            } else {
                jav.setText(String.format("                  平均分                                                         %25.2f%25.2f    " +
                        "                                              ", d.avg[1], d.avg[2]));
            }
            jptitle.add(jav);
        }
        jptitle.add(createEmptyJTA());
        JTextArea jzhu = new JTextArea();
        jzhu.setFont(new Font("仿宋", Font.PLAIN, 18));
        jzhu.setEditable(false);
        jzhu.setText("           注:当前为视图模式,无法进行编辑,返回需要在查看按当前窗口!!!" +
                "                                                  ");
        jptitle.add(jzhu);
        vboxmain.add(jptitle);
        vboxmain.repaint();
        jf.setVisible(true);
    }
    // 展示界面
    public void showFrame() {
        Font f = new Font("微软雅黑", Font.PLAIN, 16);
        UIManager.put("Menu.font", f);
        UIManager.put("MenuItem.font", f);
        UIManager.put("CheckBoxMenuItem.font", f);
        UIManager.put("ComboBox.font", f);
        UIManager.put("Label.font", f);
        UIManager.put("Button.font", f);
        UIManager.put("FileChooser.font", f);

        jf.setSize(1000, 720);   //大小
        jf.setAlwaysOnTop(true);
        jf.setResizable(false);//不可拖动
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setBackground(Color.white);
        initInterface();
        eb.init();
        m.init();

        jf.setVisible(true);
    }
    // 添加特定的空行
    public JTextArea createEmptyJTA() {
        JTextArea jkongbai = new JTextArea();
        jkongbai.setBackground(Color.LIGHT_GRAY);
        jkongbai.setEditable(false);
        jkongbai.setFont(new Font("宋体", Font.PLAIN, 4));
        jkongbai.setText("\t                                                                                               " +
                "\t                                                                                                        " +
                "\t                                                                                                        " +
                "\t                                                                                                        " +
                "\t                                                                                                        ");
        return jkongbai;
    }
    // 菜单
    class Menu {
        JMenuBar bar = new JMenuBar();
        JMenu file, edit, statistic, about, sort_asc, sort_desc, view;
        JMenuItem file_new, file_open, file_exit, file_save, file_lsave, file_guanbi, st_result, zuozhe, zhuye;
        JMenuItem s_sum, s_avg, chexiao, qinglin, nowView;
        JMenuItem course11, coures21, total1, course12, coures22, total2;
        JCheckBoxMenuItem edit_show;
        public int flag = 0;// 出现编辑框的判断信号

        // 初始化
        public void init() {
            // 第一层菜单
            file = new JMenu("文件 ");
            edit = new JMenu(" 编辑 ");
            statistic = new JMenu(" 统计 ");
            about = new JMenu(" 关于 ");
            view = new JMenu(" 查看 ");
            // 添加菜单
            bar.add(file);
            bar.add(edit);
            bar.add(view);
            bar.add(statistic);
            bar.add(about);

            // 设置文件菜单
            file_new = new JMenuItem("  新建              ");
            file_open = new JMenuItem("  导入              ");
            file_exit = new JMenuItem("  退出              ");
            file_lsave = new JMenuItem("  另存为            ");
            file_save = new JMenuItem("  保存              ");
            file_guanbi = new JMenuItem("  关闭当前窗口     ");
            // 添加文佳中菜单
            file.add(file_new);
            file.add(file_open);
            file.add(file_save);
            file.add(file_lsave);
            file.addSeparator();
            file.add(file_guanbi);
            file.add(file_exit);
            // 添加监视器
            file_exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == file_exit) {
                        if (new_file == 1) {
                            int op = JOptionPane.showConfirmDialog(jf, "  退出是否进行保存!", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
                            if (op == JOptionPane.YES_OPTION) {
                                if (now_save != 1 && new_file == 1) {
                                    if (nfile == null) {
                                        showFileSaveDialog(jf, nfile);
                                    }
                                    if (nfile != null) {
                                        writeText(nfile);
                                        System.exit(0);
                                    }
                                }
                            } else if (op == JOptionPane.NO_OPTION)
                                System.exit(0);
                        }
                        else if(new_file == 0){
                            int jop = JOptionPane.showConfirmDialog(jf, "是否退出系统!", "提示", JOptionPane.YES_NO_OPTION);
                            if(jop == JOptionPane.YES_OPTION){
                                System.exit(0);
                            }else
                                return;
                        }
                    }
                }
            }); // 退出
            file_new.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == file_new) {
                        if (new_file == 1) {
                            JOptionPane.showMessageDialog(jf, "已存在窗口!", "提示", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                        new_file = 1; // 表示新建窗口成功
                        newCreate();
                        edit_show.setSelected(true); // 出现编辑窗口
                    }
                }
            }); // 新建
            file_save.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == file_save) {
                        if (new_file == 1) {
                            if (nfile == null)
                                showFileSaveDialog(jf, nfile);
                            if (nfile != null) {
                                d.sort(3, 2);
                                writeText(nfile);
                                showData();
                                now_save = 1;
                            }
                        }
                    }
                }
            }); // 保存
            file_lsave.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == file_lsave) {
                        File old = null;
                        if(nfile != null) {
                            old = new File(nfile.getPath());
                        }
                        if (new_file == 1) {
                            showFileSaveDialog(jf, nfile);
                            if(old != null) {
                                if (nfile.getPath() != old.getPath()) {
                                    d.sort(3, 2);
                                    writeText(nfile);
                                    showData();
                                    now_save = 1;
                                }
                            }else{
                                if (nfile != null) {
                                    d.sort(3, 2);
                                    writeText(nfile);
                                    showData();
                                    now_save = 1;
                                }
                            }
                        }
                    }
                }
            }); // 另存为
            file_open.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == file_open) {
                        if (new_file == 1) {
                            JOptionPane.showMessageDialog(jf, "已有打开窗口,请先关闭窗口!", "提示", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                        showFileOpenDialog(jf);
                        if(rfile != null) {
                            new_file = 1;
                            readText(rfile);
                            nfile = new File(rfile.getName());
                            d.calculate();
                            d.analysis();
                            showData();
                        }
                        return;
                    }
                }
            }); // 导入
            file_guanbi.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == file_guanbi) {
                        if(now_number != 0) {
                            if (now_save == 0) {
                                if (new_file == 1) {
                                    if (nfile == null && rfile != null) {
                                        nfile = rfile;
                                    } else if (nfile == null)
                                        showFileSaveDialog(jf, nfile);
                                    if (nfile != null) {
                                        d.sort(3, 2);
                                        writeText(nfile);
                                    }
                                }
                            }
                            while (nfile == null && new_file == 1) {
                                int op = JOptionPane.showConfirmDialog(jf, "是否不进行保存?", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
                                if (op == JOptionPane.YES_OPTION) {
                                    break;
                                } else if (op == JOptionPane.NO_OPTION) {
                                    showFileSaveDialog(jf, nfile);
                                    if (nfile != null) {
                                        d.sort(3, 2);
                                        writeText(nfile);
                                    }
                                } else return;
                            }
                        }
                        new_file = 0;
                        now_save = 0;
                        rfile = null;
                        nfile = null;
                        now_number = 0;
                        now_view = 0;
                        initInterface();
                        edit_show.setSelected(false);
                        return;
                    }
                }
            }); // 关闭窗口

            // 设置编辑菜单
            edit_show = new JCheckBoxMenuItem("  编辑窗口      ");
            chexiao = new JMenuItem("   撤销         ");
            // qinglin = new JMenuItem("   清零         ");
            // 添加编辑菜单
            edit.add(chexiao);
            // edit.add(qinglin);
            edit.addSeparator();
            edit.add(edit_show);
            // 添加监视器
            edit_show.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (e.getSource() == edit_show) {
                        if (flag == 0) {
                            vbox1.add(jp1);
                            vbox1.add(jp2);
                            vbox1.add(Box.createVerticalStrut(5));
                            jf.add(vbox1, "South");
                        } else {
                            vbox1.removeAll();
                            vbox1.repaint();
                        }
                        flag = (flag + 1) % 2;
                        jf.setVisible(true);
                    }
                }
            }); // 编辑窗口
            chexiao.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == chexiao) {
                        if (!d.revocation()) { // 撤销判断
                            JOptionPane.showMessageDialog(jf, "无撤销内容!", "提示", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                        showData();
                        now_save = 0;
                    }
                }
            });  // 撤销

            // 编辑查看菜单
            nowView = new JMenuItem("  当前窗口      ");
            // 添加查看菜单
            view.add(nowView);
            // 添加监视器
            nowView.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    now_view = 0;
                    if (new_file == 0) {
                        return;
                    }
                    if (now_number != 0) {
                        vboxmain.removeAll();
                        showData();
                        vboxmain.repaint();
                        jf.setVisible(true);
                    }
                }
            }); // 显示当前处理数据的页面

            // 编辑统计菜单
            sort_asc = new JMenu("  升序排序        ");
            sort_desc = new JMenu("  降序排序        ");
            st_result = new JCheckBoxMenuItem("  成绩分析        ");
            s_sum = new JCheckBoxMenuItem("  总分         ");
            s_avg = new JCheckBoxMenuItem("  平均分       ");

            st_result.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == st_result) {
                        fenxi = (fenxi + 1) % 2;
                        if (new_file == 0) initInterface();
                        else if (now_number == 0 && new_file != 0) newCreate();
                        else if(now_view == 1) showView();
                        else showData();
                    }
                }
            }); // 成绩分析
            s_sum.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (e.getSource() == s_sum) {
                        now_sum = (now_sum + 1) % 2;
                        if (new_file == 0) initInterface();
                        else if (now_number == 0) newCreate();
                        else if(now_view == 1) showView();
                        else showData();
                    }
                }
            });  // 总分
            s_avg.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == s_avg) {
                        now_avg = (now_avg + 1) % 2;
                        if (new_file == 0) initInterface();
                        else if (now_number == 0) newCreate();
                        else if(now_view == 1) showView();
                        else showData();
                    }
                }
            });  // 平均分
            // 添加统计菜单
            statistic.add(s_sum);
            statistic.add(s_avg);
            statistic.add(st_result);
            statistic.addSeparator();
            statistic.add(sort_asc);
            statistic.add(sort_desc);
            course11 = new JMenuItem("  课程1         ");
            coures21 = new JMenuItem("  课程2         ");
            total1 = new JMenuItem("  总分          ");
            course12 = new JMenuItem("  课程1         ");
            coures22 = new JMenuItem("  课程2         ");
            total2 = new JMenuItem("  总分          ");
            // 添加统计菜单的子菜单
            sort_asc.add(course11);
            sort_asc.add(coures21);
            sort_asc.add(total1);
            sort_desc.add(course12);
            sort_desc.add(coures22);
            sort_desc.add(total2);
            // 添加监视器
            course11.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == course11){
                        if(new_file == 1 && now_number > 0) {
                            now_view = 1;
                            d.sort(1, 2);
                            edit_show.setSelected(false);
                            showView();
                            jf.setVisible(true);
                        }
                    }
                }
            }); // 升序,按照课程1
            coures21.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == coures21){
                        if(new_file == 1 && now_number > 0) {
                            now_view = 1;
                            edit_show.setSelected(false);
                            d.sort(2, 2);
                            showView();
                            jf.setVisible(true);
                        }
                    }
                }
            }); // 升序,按照课程2
            total1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == total1){
                        if(new_file == 1 && now_number > 0) {
                            now_view = 1;
                            edit_show.setSelected(false);
                            d.sort(3, 2);
                            s_sum.setSelected(true);
                            showView();
                            jf.setVisible(true);
                        }
                    }
                }
            });   // 升序,按照总分
            course12.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == course12){
                        if(new_file == 1 && now_number > 0) {
                            now_view = 1;
                            edit_show.setSelected(false);
                            d.sort(1, 1);
                            showView();
                            jf.setVisible(true);
                        }
                    }
                }
            }); // 降序,按照课程1
            coures22.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == coures22){
                        if(new_file == 1 && now_number > 0) {
                            now_view = 1;
                            edit_show.setSelected(false);
                            d.sort(2, 1);
                            showView();
                            jf.setVisible(true);
                        }
                    }
                }
            }); // 降序,按照课程2
            total2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == total2){
                        if(new_file == 1 && now_number > 0) {
                            now_view = 1;
                            edit_show.setSelected(false);
                            s_sum.setSelected(true);
                            d.sort(3, 1);
                            showView();
                            jf.setVisible(true);
                        }
                    }
                }
            });   // 降序,按照总分

            // 编辑关于菜单
            zuozhe = new JMenuItem("  作者             ");
            zhuye = new JMenuItem("  主页             ");
            about.add(zhuye);
            about.add(zuozhe);
            zhuye.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    vboxmain.removeAll();
                    initInterface();
                    vboxmain.repaint();
                    edit_show.setSelected(false);
                    jf.setVisible(true);
                }
            }); // 作者
            zuozhe.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == zuozhe) {
                        vboxmain.removeAll();
                        aboutInterface();
                        vboxmain.repaint();
                        edit_show.setSelected(false);
                        jf.setVisible(true);
                    }
                }
            }); // 主页

            // 展示
            jf.setJMenuBar(bar);
        }

        // 导入界面
        public void showFileOpenDialog(Component parent) {
            // 创建一个默认的文件选取器
            JFileChooser fileChooser = new JFileChooser();

            // 设置默认显示的文件夹为当前文件夹
            fileChooser.setCurrentDirectory(new File("."));
            // 设置文件选择的模式(只选文件、只选文件夹、文件和文件均可选)
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            // 设置是否允许多选
            fileChooser.setMultiSelectionEnabled(false);
            // 添加可用的文件过滤器(FileNameExtensionFilter 的第一个参数是描述, 后面是需要过滤的文件扩展名 可变参数)
            fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("zip(*.zip, *.rar)", "txt", "zip", "rar"));
            // 设置默认使用的文件过滤器
            fileChooser.setFileFilter(new FileNameExtensionFilter("text(*.txt)", "txt"));
            // 打开文件选择框(线程将被阻塞, 直到选择框被关闭)
            int result = fileChooser.showOpenDialog(parent);
            if (result == JFileChooser.APPROVE_OPTION) {
                // 如果点击了"确定", 则获取选择的文件路径
                rfile = fileChooser.getSelectedFile();
                System.out.println(rfile);
            }
        }
        // 保存界面
        public void showFileSaveDialog(Component parent, File old) {
            JFileChooser fileChooser = new JFileChooser();
            // 设置打开文件选择框后默认输入的文件名
            fileChooser.setSelectedFile(new File("sort.txt"));
            while (true) {
                // 打开文件选择框(线程将被阻塞, 直到选择框被关闭)
                int result = fileChooser.showSaveDialog(parent);
                if (result == JFileChooser.APPROVE_OPTION) {
                    // 如果点击了"保存"
                    nfile = fileChooser.getSelectedFile();
                }
                if(old != null) {
                    if (nfile.getPath() != old.getPath()) {
                        if (!validateFileName(nfile.getName())) {
                            JOptionPane.showMessageDialog(jf, "文件名不能包含下列任何字符之一:\n \\ / : * ? \" <《》 > |", "提示", JOptionPane.WARNING_MESSAGE);
                            if (old != null) nfile = new File(old.getName());
                            else nfile = null;
                            result = 0;
                        } else break;
                    } else break;
                }else{
                    if (nfile != old) {
                        if (!validateFileName(nfile.getName())) {
                            JOptionPane.showMessageDialog(jf, "文件名不能包含下列任何字符之一:\n \\ / : * ? \" <《》 > |", "提示", JOptionPane.WARNING_MESSAGE);
                            if (old != null) nfile = new File(old.getName());
                            else nfile = null;
                            result = 0;
                        } else break;
                    } else break;
                }
            }
        }
        // 写文件
        public void writeText(File writeName) {
            try {
                writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
                try (FileWriter writer = new FileWriter(writeName);
                     BufferedWriter out = new BufferedWriter(writer)
                ) {
                    out.write("编号       学号           姓名         课程1成绩      课程2成绩      总分\n");
                    for (int i = 1; i <= now_number; i++) {
                        out.write(String.format("%d    %s    %s%16.1f%16.1f%16.2f\n",
                                i, d.vDate[i].id, d.vDate[i].name, d.vDate[i].grade[1], d.vDate[i].grade[2], d.vDate[i].grade[3]));
                    }
                    out.write(String.format("平均分                              %16.2f%16.2f%14.2f", d.avg[1], d.avg[2], d.avg[3]));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("数据写入成功!");
        }
        // 读文件
        public void readText(File readName) {
            if (readName == null)
                return;
            int index = 0;
            try {
                InputStreamReader reader = new InputStreamReader(new FileInputStream(readName)); // 建立一个输入流对象reader
                BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
                String str = "", next = "";
                str = br.readLine(); // 去掉开头
                str = br.readLine();
                next = br.readLine();
                while (next != null) { // 不读最后一行
                    String[] buff = str.trim().split("\\s+");
                    d.zhuyao[++index] = new Student(buff[1], buff[2], Double.parseDouble(buff[3]), Double.parseDouble(buff[4]));
                    now_number++;
                    str = new String(next);
                    next = br.readLine(); // 一次读入一行数据
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 保存文件名限制
        public boolean validateFileName(String name) {
            if (name.indexOf(‘\\‘) != -1 || name.indexOf(‘/‘) != -1 ||
                    name.indexOf(‘:‘) != -1 || name.indexOf(‘*‘) != -1 ||
                    name.indexOf(‘?‘) != -1 || name.indexOf(‘"‘) != -1 ||
                    name.indexOf(‘<‘) != -1 || name.indexOf(‘>‘) != -1 ||
                    name.indexOf(‘|‘) != -1 || name.indexOf(‘《‘) != -1 ||
                    name.indexOf(‘》‘) != -1 || name.indexOf(‘?‘) != -1) {
                return false;
            } else {
                return true;
            }
        }
    }
    // 底层编辑窗口
    class editBottom {
        JTextField jtf1, jtf2, jtf3, jtf4, jtf5;
        JComboBox jcb1;
        JButton jb1, jb2, jb3;

        // 初始化
        public void init() {
            // 生成文本框
            jtf1 = new JTextField(8);
            jtf2 = new JTextField(8);
            jtf3 = new JTextField(6);
            jtf4 = new JTextField(6);
            jtf5 = new JTextField(6);
            // 生成下拉框
            String s[] = {"     修改      ", "     增加      ", "     删除      "};
            jcb1 = new JComboBox(s);
            // 添加
            jp1.add(jcb1);
            jp1.add(new JLabel("  编号  "));
            jp1.add(jtf5);
            jp1.add(new JLabel("  学号: "));
            jp1.add(jtf1);
            jp1.add(new JLabel(" 姓名: "));
            jp1.add(jtf2);
            jp1.add(new JLabel("   课程1成绩: "));
            jp1.add(jtf3);
            jp1.add(new JLabel("   课程2成绩: "));
            jp1.add(jtf4);
            // 生成按钮
            jb1 = new JButton("   确定   ");
            jb2 = new JButton("   清除   ");
            jb3 = new JButton("   例子   ");
            jp2.add(jb3);
            jp2.add(new JLabel("            "));
            jp2.add(jb2);
            jp2.add(new JLabel("            "));
            jp2.add(jb1);

            // 监听器
            jb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == jb1) {
                        if (now_view == 0) {
                            if (jcb1.getSelectedItem() == "     修改      ") {
                                try {
                                    int p = Integer.parseInt(jtf5.getText());
                                    if (p > now_number) {
                                        JOptionPane.showMessageDialog(jf, "不存在该编号!", "提示", JOptionPane.WARNING_MESSAGE);
                                        jtf5.setText("");
                                        return;
                                    }
                                    if (p >= 10) {
                                        JOptionPane.showMessageDialog(jf, "暂时不支持编号10以上的数据!", "提示", JOptionPane.WARNING_MESSAGE);
                                        jtf5.setText("");
                                        return;
                                    }
                                    if (!jtf3.getText().equals("")) {
                                        if (Double.parseDouble(jtf3.getText()) < 0 || Double.parseDouble(jtf3.getText()) > 100) {
                                            JOptionPane.showMessageDialog(jf, "课程1输入的成绩大于100(小于0)!", "提示", JOptionPane.WARNING_MESSAGE);
                                            jtf3.setText("");
                                            return;
                                        }
                                    }
                                    if (!jtf4.getText().equals("")) {
                                        if (Double.parseDouble(jtf4.getText()) < 0 || Double.parseDouble(jtf4.getText()) > 100) {
                                            JOptionPane.showMessageDialog(jf, "课程2输入的成绩大于100(小于0)!", "提示", JOptionPane.WARNING_MESSAGE);
                                            jtf4.setText("");
                                            return;
                                        }
                                    }
                                    if(jtf4.getText().equals("") && jtf1.getText().equals("") && jtf2.getText().equals("") && jtf3.getText().equals("")){
                                        JOptionPane.showMessageDialog(jf, "请输入修改数据!", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    d.record(1, p);
                                    if (!jtf3.getText().equals("")) d.zhuyao[p].grade[1] = Double.parseDouble(jtf3.getText());
                                    if (!jtf4.getText().equals("")) d.zhuyao[p].grade[2] = Double.parseDouble(jtf4.getText());
                                    if (!jtf2.getText().equals("")) d.zhuyao[p].name = new StringBuffer(jtf2.getText());
                                    if (!jtf1.getText().equals("")) d.zhuyao[p].id = new StringBuffer(jtf1.getText());
                                    d.calculate();
                                    d.analysis();
                                    now_save = 0;
                                    showData();
                                    jtf1.setText("");
                                    jtf2.setText("");
                                    jtf3.setText("");
                                    jtf4.setText("");
                                    jtf5.setText("");
                                } catch (Exception d) {
                                    JOptionPane.showMessageDialog(jf, "输入有误!请重新输入!", "提示", JOptionPane.WARNING_MESSAGE);
                                    jtf3.setText("");
                                    jtf4.setText("");
                                    jtf5.setText("");
                                    return;
                                }
                            } else if (jcb1.getSelectedItem() == "     增加      ") {
                                try {
                                    if (now_number + 1 >= 10) {
                                        JOptionPane.showMessageDialog(jf, "暂时不支持编号10以上的数据!", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    if (jtf1.getText().equals("")) {
                                        JOptionPane.showMessageDialog(jf, "请输入学生学号!", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    if (jtf2.getText().equals("")) {
                                        JOptionPane.showMessageDialog(jf, "请输入学生姓名!", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    if (jtf3.getText().equals("")) {
                                        JOptionPane.showMessageDialog(jf, "请输入课程1的成绩!", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    } else if (!jtf3.getText().equals("")) {
                                        if (Double.parseDouble(jtf3.getText()) < 0 || Double.parseDouble(jtf3.getText()) > 100) {
                                            JOptionPane.showMessageDialog(jf, "课程1输入的成绩大于100(小于0)!", "提示", JOptionPane.WARNING_MESSAGE);
                                            jtf3.setText("");
                                            return;
                                        }
                                    }
                                    if (jtf4.getText().equals("")) {
                                        JOptionPane.showMessageDialog(jf, "请输入课程2的成绩!", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    } else if (!jtf4.getText().equals("")) {
                                        if (Double.parseDouble(jtf4.getText()) < 0 || Double.parseDouble(jtf4.getText()) > 100) {
                                            JOptionPane.showMessageDialog(jf, "课程2输入的成绩大于100(小于0)!", "提示", JOptionPane.WARNING_MESSAGE);
                                            jtf4.setText("");
                                            return;
                                        }
                                    }
                                    now_number++;
                                    d.record(2, now_number);
                                    if (!jtf3.getText().equals(""))
                                        d.zhuyao[now_number].grade[1] = Double.parseDouble(jtf3.getText());
                                    else {
                                        JOptionPane.showMessageDialog(jf, "请输入课程1的成绩", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    if (!jtf4.getText().equals(""))
                                        d.zhuyao[now_number].grade[2] = Double.parseDouble(jtf4.getText());
                                    else {
                                        JOptionPane.showMessageDialog(jf, "请输入课程2的成绩", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    if (!jtf2.getText().equals(""))
                                        d.zhuyao[now_number].name = new StringBuffer(jtf2.getText());
                                    else {
                                        JOptionPane.showMessageDialog(jf, "请输入学生姓名", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    if (!jtf1.getText().equals(""))
                                        d.zhuyao[now_number].id = new StringBuffer(jtf1.getText());
                                    else {
                                        JOptionPane.showMessageDialog(jf, "请输入学生学号", "提示", JOptionPane.WARNING_MESSAGE);
                                        return;
                                    }
                                    d.calculate();
                                    d.analysis();
                                    now_save = 0;
                                    showData();
                                    jtf1.setText("");
                                    jtf2.setText("");
                                    jtf3.setText("");
                                    jtf4.setText("");
                                    jtf5.setText("");
                                }catch (Exception d){
                                    JOptionPane.showMessageDialog(jf, "输入有误!请重新输入!", "提示", JOptionPane.WARNING_MESSAGE);
                                    jtf3.setText("");
                                    jtf4.setText("");
                                    jtf5.setText("");
                                    return;
                                }
                            } else if (jcb1.getSelectedItem() == "     删除      ") {
                                try {
                                    int p = Integer.parseInt(jtf5.getText());
                                    if (p > now_number || p <= 0) {
                                        JOptionPane.showMessageDialog(jf, "不存在该编号!", "提示", JOptionPane.WARNING_MESSAGE);
                                        jtf5.setText("");
                                        return;
                                    }
                                    d.record(3, p);
                                    d.delete(p);
                                    now_save = 0;
                                    if (now_number == 0) newCreate();
                                    else showData();
                                } catch (Exception d) {
                                    JOptionPane.showMessageDialog(jf, "输入有误!请重新输入!", "提示", JOptionPane.WARNING_MESSAGE);
                                    jtf3.setText("");
                                    jtf4.setText("");
                                    jtf5.setText("");
                                    return;
                                }
                            }
                        } else if (now_view == 1) {
                            JOptionPane.showMessageDialog(jf, "视图模式下无法编辑数据!", "提示", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                    }
                }
            });// 确定
            jb2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == jb2) {
                        jtf1.setText("");
                        jtf2.setText("");
                        jtf3.setText("");
                        jtf4.setText("");
                    }
                }
            });// 清除
            jb3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == jb3) {
                        int op = JOptionPane.showConfirmDialog(jf, "是否使用例子覆盖原有数据?", "提示", JOptionPane.YES_NO_OPTION);
                        if (op == JOptionPane.YES_OPTION) {
                            d.oneExample();
                            now_save = 0;
                            showData();
                        }
                    }
                }
            });// 例子
        }
    }
    // 数据类
    class Student {
        public StringBuffer id;
        public StringBuffer name;
        public double[] grade = new double[4];

        Student() {
        }

        Student(String id, String name, double grade1, double grade2) {
            this.id = new StringBuffer(id);
            this.name = new StringBuffer(name);
            this.grade[1] = grade1;
            this.grade[2] = grade2;
            this.grade[3] = grade1 + grade2;
        }

        Student(Student s) {
            this.id = new StringBuffer(s.id.toString());
            this.name = new StringBuffer(s.name.toString());
            this.grade[1] = s.grade[1];
            this.grade[2] = s.grade[2];
            this.grade[3] = s.grade[1] + s.grade[2];
        }

        public String toString(){
            return id + " " + name + " " + grade[1] + " " + grade[2]+ " " + grade[3];
        }

    }
    // 数据处理
    class Data {
        // 主要数据
        Student[] zhuyao = new Student[20];
        public double avg[] = new double[4];
        public int[] numYouxiu, numLianghao, numJige, numBujige;
        public double[] youxiuB, lianghaoB, jigeB, bujigeB;
        // 建立视图的数据
        Student[] vDate = new Student[20];
        // 撤销记录数据
        Student[] rData = new Student[100];
        public int now_r = 0; // 记录数
        public int[] rcaozuo = new int[100];
        public int[] bianhao = new int[100];

        // 例子
        public void oneExample() {
            now_number = 7;
            zhuyao[1] = new Student("2018211001", "张铁三", 67, 78);
            zhuyao[2] = new Student("2018211002", "李搞四", 99, 98);
            zhuyao[3] = new Student("2018211003", "王老五", 52, 88);
            zhuyao[4] = new Student("2018211004", "兔歪六", 82, 74);
            zhuyao[5] = new Student("2018211005", "蛇数七", 66, 55);
            zhuyao[6] = new Student("2018211006", "马爸八", 44, 59);
            zhuyao[7] = new Student("2018211007", "羊法九", 84, 85);
            zhuyao[8] = new Student("2018211008", "猴老十", 64, 91);
            zhuyao[9] = new Student("2018211009", "鸡十一", 78, 77);
            calculate();
            analysis();
        }
        // 分析模块
        public void analysis() {
            numYouxiu = new int[2];
            numLianghao = new int[2];
            numJige = new int[2];
            numBujige = new int[2];
            youxiuB = new double[2];
            lianghaoB = new double[2];
            jigeB = new double[2];
            bujigeB = new double[2];
            for (int i = 1; i <= now_number; i++) {
                if (zhuyao[i].grade[1] >= 85) numYouxiu[0]++;
                else if (zhuyao[i].grade[1] >= 70) numLianghao[0]++;
                else if (zhuyao[i].grade[1] >= 60) numJige[0]++;
                else if (zhuyao[i].grade[1] < 60) numBujige[0]++;
                if (zhuyao[i].grade[2] >= 85) numYouxiu[1]++;
                else if (zhuyao[i].grade[2] >= 70) numLianghao[1]++;
                else if (zhuyao[i].grade[2] >= 60) numJige[1]++;
                else if (zhuyao[i].grade[2] < 60) numBujige[1]++;
            }
            for (int i = 0; i < 2; i++) {
                youxiuB[i] = (double) numYouxiu[i] / now_number;
                lianghaoB[i] = (double) numLianghao[i] / now_number;
                jigeB[i] = (double) numJige[i] / now_number;
                bujigeB[i] = (double) numBujige[i] / now_number;
            }
        }
        // 计算总分和平均分
        public void calculate() {
            double sum1 = 0, sum2 = 0, sum3 = 0;
            for (int i = 1; i <= now_number; i++) {
                zhuyao[i].grade[3] = zhuyao[i].grade[1] + zhuyao[i].grade[2];
                sum1 += zhuyao[i].grade[1];
                sum2 += zhuyao[i].grade[2];
                sum3 += zhuyao[i].grade[3];
            }
            avg[1] = sum1 / now_number;
            avg[2] = sum2 / now_number;
            avg[3] = sum3 / now_number;
        }
        // 记录步骤
        public void record(int x, int pos) {
            rcaozuo[++now_r] = x;
            bianhao[now_r] = pos;
            rData[now_r] = new Student(zhuyao[pos].id.toString(), zhuyao[pos].name.toString(), zhuyao[pos].grade[1], zhuyao[pos].grade[2]);
        }
        // 删除
        public void delete(int pos) {
            for (int i = pos; i <= now_number; i++) {
                zhuyao[i] = new Student(zhuyao[i + 1]);
            }
            now_number--;
            calculate();
            analysis();
        }
        // 撤销模块
        public boolean revocation() {
            if (now_r <= 0)
                return false;
            // if(bianhao[now_r] > now_number){ for (; bianhao[now_r] > now_number ; now_r--) { } }
            if (rcaozuo[now_r] == 1) { // 撤销修改
                zhuyao[bianhao[now_r]] = new Student(rData[now_r]);
            } else if (rcaozuo[now_r] == 2) { // 撤销增加
                now_number--;
            } else if (rcaozuo[now_r] == 3) { // 撤销删除
                now_number++;
                for (int i = now_number; i > bianhao[now_r]; i--) {
                    zhuyao[i] = new Student(zhuyao[i - 1]);
                }
                zhuyao[bianhao[now_r]] = new Student(rData[now_r]);
            }
            calculate();
            analysis();
            now_r--;
            return true;
        }
        // 排序
        public void sort(int flag, int order){
            // flag = 1,按照课程1成绩排序
            // flag = 2,按照课程2成绩排序
            // flag = 3,按照总分排序
            // order = 1, 升序,order = 2, 降序
            Student temp;
            for (int i = 1; i <= now_number ; i++) {
                vDate[i] = new Student(zhuyao[i]);
            }
            for (int i = 1; i < now_number; i++) {
                for (int j = i+1; j <= now_number; j++) {
                    if(flag == 1){
                        if(judge(vDate[i].grade[1], vDate[j].grade[1], order)){
                            temp = new Student(vDate[i]);
                            vDate[i] = new Student(vDate[j]);
                            vDate[j] = new Student(temp);
                        }
                    } else if(flag == 2){
                        if(judge(vDate[i].grade[2], vDate[j].grade[2], order)){
                            temp = new Student(vDate[i]);
                            vDate[i] = new Student(vDate[j]);
                            vDate[j] = new Student(temp);
                        }
                    } else if(flag == 3){
                        if(judge(vDate[i].grade[3], vDate[j].grade[3], order)){
                            temp = new Student(vDate[i]);
                            vDate[i] = new Student(vDate[j]);
                            vDate[j] = new Student(temp);
                        }
                    }
                }
            }
            for (int i = 1; i <= now_number ; i++) {
                System.out.println(vDate[i]);
            }

        }
        // 判断大小
        public boolean judge(double a, double b, int order) {
            if (order == 1)  return a > b;
            else  return a < b;
        }
    }
}

public class SGC {
    public static void main(String[] args) {
        Interface itf = new Interface();
        itf.showFrame();
    }
}

 

学生成绩管理系统(java swing)

标签:frame   analysis   rgs   panel   name   display   exception   select   计算   

原文地址:https://www.cnblogs.com/shish/p/12819438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!