|
40Java金币
本帖最后由 kuku1166 于 2015-12-13 08:11 编辑
下面是我的关于按钮和单选项的代码,想实现每按一次按钮就获取一次选项值,每次按下按钮能获取不同的单选项值,请问要怎么改?
private String getSelected(){
String select = "a";
if(jRadioButton2.isSelected()){
select = "b";
}else if(jRadioButton3.isSelected()){
select = "c";
}else if(jRadioButton4.isSelected()){
select = "d";
}
return select;
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
for(int i=0;i<5;i++)
{
String selected = getSelected();
if(question.getResult(i).equalsIgnoreCase(selected)){ //判断所选的答案和正确答案是否相同
currentGrades+=20;
jLabel12.setText(currentGrades+"");
jLabel13.setText("");
jLabel2.setText(question.gettitle(i+1));
jTextArea1.setText(question.getContent(i+1));
jRadioButton1.setText(question.getSelectiona(i+1));
jRadioButton2.setText(question.getSelectionb(i+1));
jRadioButton3.setText(question.getSelectionc(i+1));
jRadioButton4.setText(question.getSelectiond(i+1));
jRadioButton1.setSelected(true);
if(currentGrades==100){
JOptionPane.showConfirmDialog(this, "恭喜你答对了全部题目!", "答题结果", JOptionPane.OK_OPTION);
}
}else {
jButton1.setEnabled(false);
jLabel13.setText(question.getResult(i));
JOptionPane.showConfirmDialog(this, "回答错误!\n正确答案是: "+question.getResult(i)+"\n当前得分:"+currentGrades, "答题结果", JOptionPane.OK_OPTION);
break;
}
}
// TODO add your handling code here:
}
问题补充:现在就是只能选择第一题的选项,然后后面的第二题会默认是第一题的答案,直接报错,不好选选项
|
|