简体   繁体   中英

Radio button .isSelected() method doesn't return right response

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");
                }
        }
    }
 }

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 ++;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM