[英]Radio button .isSelected() method doesn't return right response
我正在创建一个基本的 Java GUI 琐事游戏,其中一个人选择一个单选按钮,然后单击一个按钮。 然后程序检查是否选择了正确的单选按钮。 它根据选择的单选按钮显示一条消息。 出于某种原因,即使您选择了第一个问题的正确单选按钮 select,即使您选择了正确的单选按钮,它也会显示“false”。
更令人困惑的是,消息“假”来自第二个问题。 如果你弄错了,第一个问题会显示“False”。 我很困惑为什么会发生这种情况,我需要一些帮助。
private static class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String recieve = e.getActionCommand ();
if (recieve.equals("name")) {
String input = txtName.getText ();
lblTitle2.setText(input + " want's to be a Millionare!");
}
frame1.setVisible (false);
frame2.setVisible(true);
int counter = 0;
if (recieve.equals("q1")) {
A.setEnabled(true);
B.setEnabled(true);
C.setEnabled(true);
D.setEnabled(true);
Rbtn1.setEnabled(false);
lblFill1.setText("What is on the Canadian Quarter?");
A.setText("Caribou");
B.setText("Deer");
C.setText("Schooner");
D.setText("Seal");
}
if (recieve.equals("final") && counter==0) {
if (A.isSelected()) {
lblScore.setText("Correct");
Rbtn2.setEnabled(true);
A.setEnabled(false);
B.setEnabled(false);
C.setEnabled(false);
D.setEnabled(false);
}
else {
lblScore.setText("False");
}
}
counter++;
if (recieve.equals("q2")) {
A.setEnabled(true);
B.setEnabled(true);
C.setEnabled(true);
D.setEnabled(true);
Rbtn2.setEnabled(false);
lblFill1.setText("The side of a coin with the face is called the?");
A.setText("Tail");
B.setText("Head");
C.setText("Face");
D.setText("Up");
}
if (recieve.equals("final") && counter==1 ) {
if (B.isSelected()) {
lblScore.setText("correct");
Rbtn3.setEnabled(true);
}
else {
lblScore.setText("false");
}
}
}
}
将柜台声明放在外面:
private static class ButtonHandler implements ActionListener
{
int counter = 0;
public void actionPerformed (ActionEvent e)
{
放在
counter ++;
在一个成功的答案之后,比如
if (A.isSelected()) {
counter ++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.