有只熊463 2023-01-05 15:57 采纳率: 85.3%
浏览 136
已结题

有人可以详细解释一下这段代码吗

辛苦各位了TT


        JOptionPane.showMessageDialog(this,"测试得分:" + score + "\n测试评语:" + comment);
        System.exit(0);
    }
  • 写回答

1条回答 默认 最新

  • 於黾 2023-01-05 16:07
    关注
    
     
    public class TestFrame extends JFrame {//窗体类,继承了JFrame
        Exam exam;//一些成员变量
        Student student;
        JPanel[] panels;
        JLabel timeLabel;
        JButton button;
        boolean timeOver = false;
     
        public TestFrame(Student student) {//构造函数
            this.student = student;//形参保存至成员变量
            exam = Database.getOneExam();//初始化试卷
            setLayout(null);//挂起布局逻辑
            setTitle("在线心理测试");//设置窗口标题
            setResizable(false);//不可修改窗口大小
            setLocationRelativeTo(null);//设置居中显示
            setSize(700, 640);//设置窗口大小
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮退出程序
            initComponents();//执行函数
            new TimingThread().start();//启动timer
            setVisible(true);//设置可见
        }
     
        //初始化控件
        //这都是窗体布局,你亲自运行一下看看效果会比较好,干讲没什么概念
        public void initComponents() {
            JPanel tipPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            tipPanel.setBounds(0,0,getWidth(),50);
            add(tipPanel);
     
            timeLabel = new JLabel("考试时间:");
            timeLabel.setFont(new Font("微软雅黑",Font.BOLD,24));
            tipPanel.add(timeLabel);
     
            //盒子+滚动布局
            Box box = Box.createVerticalBox();
            JScrollPane scrollPane = new JScrollPane(box);
            scrollPane.setBounds(0,50,getWidth() - 20,getHeight() - 150);
            add(scrollPane);
     
            //设置20个面板装20道题
            ArrayList<Question> questions = exam.getQuestions();
            panels = new JPanel[20];
            for (int i = 0; i < panels.length; i++) {
                JPanel panel = new JPanel(new FlowLayout());
                panels[i] = panel;
                box.add(panel);
                //获取本题
                Question question = questions.get(i);
                //题目标题标签
                JLabel label = new JLabel((i + 1) + "." + question.getTitle());
                panel.add(label);
                //创建按钮组
                ButtonGroup group = new ButtonGroup();
                JRadioButton[] boxes = new JRadioButton[4];
                for (int j = 0; j < 4; j++) {
                    JRadioButton radioButton = new JRadioButton(question.getOption(j));
                    group.add(radioButton);
                    panel.add(radioButton);
                    boxes[j] = radioButton;
                }
            }
     
            button = new JButton("交卷");
            button.setBounds(280,560,100,30);
            add(button);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    handIn();//点交卷按钮执行的函数
                }
            });
        }
     
        //交卷
        public void handIn(){//这里注释已经够多了,具体哪句看不懂可以粘到网上搜一搜
            ArrayList<Question> questions = exam.getQuestions();
            for (int i = 0; i < panels.length; i++) {
                JPanel panel = panels[i];
                //获取题目
                Question question = questions.get(i);
                //获取面板的子控件
                Component[] components = panel.getComponents();
                for (int j = 1; j < components.length; j++) {
                    JRadioButton radioButton = (JRadioButton) components[j];
                    if (radioButton.isSelected()){
                        //设置问题答案
                        question.setAnswer(j - 1);
                        break;
                    }
                }
            }
     
            if (!timeOver){
                for (Question question : exam.getQuestions()) {
                    if(question.getScore() == -1){
                        if(JOptionPane.showConfirmDialog(this,"还有未答的题,确定要交卷吗?") != 0){
                            return;
                        }else {
                            break;
                        }
                    }
                }
            }
     
            int score = 0;
            for (Question question : exam.getQuestions()) {
                if (question.getScore() != -1){
                    score += question.getScore();
                }
            }
     
            String comment;
            if (score < 10){
                comment = "重度抑郁";
            }else if (score < 30){
                comment = "中度抑郁";
            }else if (score < 50){
                comment = "轻度抑郁";
            }else if (score < 70){
                comment = "心情不好";
            }else{
                comment = "身心健康";
            }
            student.setScore(score);
            student.setComment(comment);
            //添加测评记录
            Database.students.add(student);
            Database.writeData();
            JOptionPane.showMessageDialog(this,"测试得分:" + score + "\n测试评语:" + comment);
            System.exit(0);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月13日
  • 已采纳回答 1月5日
  • 修改了问题 1月5日
  • 创建了问题 1月5日

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢