我不知道這是一個愚蠢的問題,我有或什麼,但對於我的生活我無法找出解決方案。我正在寫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");
}
});
@mKorbel What * is * the question? OP - 爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/),並請回答我的問題(致mKorbel)。 –