for (int j = 0; j < 4; j++) { final int index = j; answerButtons[index].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { rightanswer =getAnswer(i); i++; next.setEnabled(true); if (answerButtons[index].getText().equals(rightanswer)) { right++; score.setText(right + "/" + i); show.setText("correct!"); answerButtons[index].setBackground(Color.green); } else { show.setText("wrong"); score.setText(right + "/" + i); answerButtons[index].setBackground(Color.pink); } rightanswer =getAnswer(i); enabled(false); next.setEnabled(true); } }); }为什么正确答案不变绿
时间: 2024-01-19 10:03:09 浏览: 31
根据你提供的代码,正确答案没有变绿的原因可能是因为你在每次点击答案按钮时都会重新获取正确答案并赋值给`rightanswer`变量。因此,即使答案按钮的文本与正确答案相同,也不会变绿色,因为此时`rightanswer`变量已经被更新为下一道题的正确答案了。
要解决这个问题,你可以在每次点击答案按钮时,先将正确答案保存在一个临时变量中,然后再将`rightanswer`变量更新为下一道题的正确答案。同时,在判断答案是否正确时,使用临时变量来比较。这样,即使`rightanswer`变量被更新了,也不会影响判断结果。
相关问题
for(int j=0;j<4;j++) { final int index = j; answerButtons[index].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { i++; next.setEnabled(true); if (answerButtons[index].getText().equals(rightanswer)) { right++; score.setText(right + "/" + i); show.setText("correct!"); answerButtons[index].setBackground(Color.green); } else { show.setText("wrong"); score.setText(right + "/" + i); answerButtons[index].setBackground(Color.pink); } rightanswer = getAnswer(i); enabled(false); next.setEnabled(true); }为什么下一个题的时候不能选择了
根据提供的代码,你在每个答案按钮的 ActionListener 中都有执行 `enabled(false)` 的操作,这会使得所有的答案按钮都被禁用(即不能选择)。因此,在进入下一题的时候,你需要将答案按钮重新启用,可以在下一题按钮的 ActionListener 中添加如下代码:
```
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 答案按钮重新启用
for (int j = 0; j < 4; j++) {
answerButtons[j].setEnabled(true);
answerButtons[j].setBackground(null); // 将按钮背景色还原
}
// 其他逻辑代码
// ...
}
});
```
这样,在进入下一题时,所有的答案按钮就会被重新启用。
for(int j=0;j<4;j++) { answerButtons[j].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { i++; next.setEnabled(true); if (answerButtons[j].getText().equals(rightanswer)) { right++; score.setText(right + "/" + i); show.setText("correct!"); answerButtons[j].setBackground(Color.green); } else { show.setText("wrong"); score.setText(right + "/" + i); answerButtons[j].setBackground(Color.pink); } rightanswer = getAnswer(i); enabled(false); next.setEnabled(true); } });}错在哪了
这段代码中存在一个常见的陷阱:在循环内部创建 ActionListener 时,由于闭包特性,所有的 ActionListener 都将共享相同的变量 j,这将导致 ActionEvent 事件中使用的 j 变量总是等于循环结束时的最终值,而不是期望的当前值。
为了避免这个问题,你需要使用一个 final 变量来保存循环变量的值,例如:
```
for (int j = 0; j < 4; j++) {
final int index = j;
answerButtons[index].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i++;
next.setEnabled(true);
if (answerButtons[index].getText().equals(rightanswer)) {
right++;
score.setText(right + "/" + i);
show.setText("correct!");
answerButtons[index].setBackground(Color.green);
} else {
show.setText("wrong");
score.setText(right + "/" + i);
answerButtons[index].setBackground(Color.pink);
}
rightanswer = getAnswer(i);
enabled(false);
next.setEnabled(true);
}
});
}
```
在这个版本的代码中,我们创建了一个 final 变量 index 来保存循环变量 j 的值,并在 ActionListener 中使用它来访问 answerButtons 数组的元素。这样,每个 ActionListener 都会使用它自己的 index 值,而不会受到循环变量的影响。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)