2012-12-07 36 views
1

我不知道這是一個愚蠢的問題,我有或什麼,但對於我的生活我無法找出解決方案。我正在寫20個問題的測驗計劃。該程序詢問每個問題,並且用戶有5個選擇,即5個JRadio按鈕,一旦用戶選擇一個答案,他可以點擊下一個問題或前一個問題查看問題。我遇到的問題是,一旦用戶回答問題並點擊下一個選擇的上一個單選按鈕保持選中狀態,我的意思是如果問題1的答案A和問題2的下一個選擇A將被選中,依此類推。 5個單選按鈕位於一個按鈕組中,我使用清除選擇方法來清除其工作正常的選擇,除非用戶在點擊下一個按鈕後點擊上一個按鈕來查看問題,以便繼續所有選擇時清除,讓我們說用戶回答10個問題,回到問題編號3,然後回到問題10,其間的所有問題都將被清除。JRadio按鈕選擇問題

我將在下面添加下一個和以前的實現。 任何想法將不勝感激。

//實現下一個按鈕

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"); 
     } 
    }); 
+1

@mKorbel What * is * the question? OP - 爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/),並請回答我的問題(致mKorbel)。 –

回答

2

正如你所描述的,當用戶點擊Next按鈕,單選按鈕被清除,如果你不救用戶的選擇,當然用戶不能檢查他以前的答案。

所以我認爲你需要創建一個HashMap,它使用questionIndex作爲鍵,answer作爲值來存儲用戶的選擇。每次用戶點擊下一個按鈕時,只需將用戶的選擇放入帶有問題ID的散列表中即可。當用戶點擊上一個按鈕時,只需獲取上一個問題的索引並從散列表中獲取答案,並選擇相應的單選按鈕。

+0

對不起,我沒有提到,我已經有一個方法,那樣做,如果我刪除清除選擇方法程序工作正常所有的答案都保存問題出現時,用戶下一個命中,並得到一個以前沒有回答的問題,當他得到這樣的問題時,所有單選按鈕都應該是空白的,但出於某種原因,其中一個按鈕被選中。 – rafaelzm2000

+0

在這種情況下,我認爲你需要檢查當前的問題是否已經回答過,例如,你可以使用當前問題的ID來檢查你的散列表,看看散列表是否包含這樣的ID,如果它包含,只是得到答案並選擇單選按鈕,如果沒有,只需清除單選按鈕。 – bhuang3