英文:
Radio button .isSelected() method doesn't return right response
问题
private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String receive = e.getActionCommand();
if (receive.equals("name")) {
String input = txtName.getText();
lblTitle2.setText(input + " wants to be a Millionaire!");
}
frame1.setVisible(false);
frame2.setVisible(true);
int counter = 0;
if (receive.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 (receive.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 (receive.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 (receive.equals("final") && counter == 1) {
if (B.isSelected()) {
lblScore.setText("Correct");
Rbtn3.setEnabled(true);
} else {
lblScore.setText("False");
}
}
}
}
英文:
I am creating a basic Java GUI trivia game where one selects a radio button, and clicks on a button. The program then checks if the correct radio button was selected. It displays a message based on what radio button is selected. For some reason, even when you select the correct radio button for the first question, it displays "false" even if you selected the correct radio button.
More confusing is that the message "false" is from the second question. The first question displays "False" if you get it wrong. I am quite confused about why this is happening and I would like some help.
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");
}
}
}
}
答案1
得分: 0
将计数器声明放在外面:
private static class ButtonHandler implements ActionListener {
int counter = 0;
public void actionPerformed(ActionEvent e) {
在成功的答案后加入
counter++;
例如:
if (A.isSelected()) {
counter++;
英文:
Put the counter declaration outside:
private static class ButtonHandler implements ActionListener
{
int counter = 0;
public void actionPerformed (ActionEvent e)
{
Put the
counter ++;
after a succesfull answer, like
if (A.isSelected()) {
counter ++;
专注分享java语言的经验与见解,让所有开发者获益!
评论