java - 单选按钮选择问题

标签 java swing jradiobutton buttongroup

我不知道这是我遇到的一个愚蠢的问题还是什么,但我一生都找不到解决方案。我正在编写一个包含 20 个问题的测验程序。该程序询问每个问题,用户有 5 个选择,即 5 个 JRadio 按钮,一旦用户选择一个答案,他可以点击下一步转到下一个问题或上一个问题以查看问题。我遇到的问题是,一旦用户回答问题并点击下一个,所选的上一个单选按钮将保持选中状态,我的意思是,如果问题 1 的答案 A 并点击下一个选项 A 将为问题 2 选择,依此类推。 5 个单选按钮位于一个按钮组中,我使用清除选择方法来清除选择,它工作正常,除非用户单击上一个按钮来查看问题,一旦他单击下一个按钮继续所有选择,那么让假设用户回答了 10 个问题,然后返回查看第 3 个问题,一旦他点击“下一步”返回到第 10 个问题,中间的所有问题都将被清除。

我将在下面添加下一个和上一个实现。 任何想法将不胜感激。

//实现下一个按钮

  nextBT.setPreferredSize(new Dimension(70, 30));
    nextBT.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {

            int nextQuestion = -1;
            boolean answer = getAnswer(currentQuestion, selectedButton()).equals(getCorrectAnswer(currentQuestion));
            questionHistory[currentHistoryIndex][0] = currentQuestion;
            questionHistory[currentHistoryIndex][1] = selectedButton();

            if (currentHistoryIndex == maxHistoryIndex) {

                //generate next question number to use
                int currentLevel = currentQuestion / 25;
                int nextLevel = currentLevel;
                if (answer) {
                    if (currentLevel < 3) {
                        nextLevel++;
                    }
                } else {
                    if (currentLevel > 0) {
                        nextLevel--;
                    }
                }
                while (true) {
                    int k = 0;
                    Random randomNum = new Random();
                    nextQuestion = nextLevel * 25 + (int) (randomNum.nextInt(25));
                    for (k = 0; k < maxHistoryIndex; k++) {
                        if (questionHistory[k][0] == nextQuestion) {
                            break;
                        }
                    }
                    if (k == maxHistoryIndex) {
                        break;
                    }
                }
                currentHistoryIndex++;
                maxHistoryIndex++;
                if (maxHistoryIndex == 19) {
                    nextBT.setEnabled(false);

                } else {
                    nextBT.setEnabled(true);
                }

            } else {
                // returning to question already on list  
                currentHistoryIndex++;

                nextQuestion = questionHistory[currentHistoryIndex][0];
                int nextAnswer = questionHistory[currentHistoryIndex][1];
                setSelectedButton(nextAnswer);
            }

            if (currentHistoryIndex == 19) {
                nextBT.setEnabled(false);
            }

            currentQuestion = nextQuestion;
            questionHistory[currentHistoryIndex][0] = currentQuestion;
            questionHistory[currentHistoryIndex][1] = selectedButton();

            question.setText(questions[currentQuestion * 7]);
            rb1.setText(questions[(currentQuestion * 7) + 1]);
            rb2.setText(questions[(currentQuestion * 7) + 2]);
            rb3.setText(questions[(currentQuestion * 7) + 3]);
            rb4.setText(questions[(currentQuestion * 7) + 4]);
            rb5.setText(questions[(currentQuestion * 7) + 5]);

            previousBT.setEnabled(true);

            //setSelectedButton(questionHistory[currentHistoryIndex][1]);
            questionCountLB.setText("Question " + (currentHistoryIndex + 1) + " of 20");


            //if(bg.isSelected()){



            bg.clearSelection();
        }

    }); 

//实现上一个按钮

    previousBT.setPreferredSize(new Dimension(120, 30));
    previousBT.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {

            nextBT.setEnabled(true);
            questionHistory[currentHistoryIndex][1] = selectedButton();

            currentHistoryIndex--;
            if (currentHistoryIndex == 0) {
                previousBT.setEnabled(false);
            }
            if (currentHistoryIndex > 0) {
                previousBT.setEnabled(true);
            }
            int nextQuestion = questionHistory[currentHistoryIndex][0];
            currentQuestion = nextQuestion;
            question.setText(questions[currentQuestion * 7]);
            rb1.setText(questions[(currentQuestion * 7) + 1]);
            rb2.setText(questions[(currentQuestion * 7) + 2]);
            rb3.setText(questions[(currentQuestion * 7) + 3]);
            rb4.setText(questions[(currentQuestion * 7) + 4]);
            rb5.setText(questions[(currentQuestion * 7) + 5]);

            setSelectedButton(questionHistory[currentHistoryIndex][1]);
            questionCountLB.setText("Question " + (currentHistoryIndex + 1) + " of 20");
        }
    });

最佳答案

正如您所描述的,当用户点击下一步按钮时,单选按钮将被清除,如果您不保存用户的选择,用户当然无法检查他之前的答案。

所以我认为你需要创建一个HashMap,它使用questionIndex作为键,使用answer作为值,来存储用户的选择。每次用户点击下一步按钮时,只需将用户的选择与问题 ID 一起放入 HashMap 中即可。当用户点击上一个按钮时,只需获取上一个问题的索引并从 HashMap 中获取答案,然后选择相应的单选按钮即可。

关于java - 单选按钮选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13769193/

相关文章:

java - 如何给多个按钮提供不同的命令

java - 基本单选按钮问题 - java

java - 如何使用 RestTemplate 发布 XML

java - 强制顺序优先(右结合)

java - 如何比较两个不同大小的数组列表并检查元素的顺序

java - setter 似乎无法在主方法中工作

java - 为 JFormattedTextField 分配一个浮点型

java - 当选择/未选择单选按钮时如何隐藏文本字段?

java - 我无法确定此 JRadioButton 功能的语义

java - 等待 selenium webdriver 直到加载完成