2

I'm trying to make a "Simple" quiz in Java using a JFrame. Basically long story short... When the user clicks the "NEXT" button after question 2, it doesn't show the next question...

How can I get the code to proceed to question 3?

CODE

  import javax.swing.DefaultListModel;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class SimpleQuiz implements KeyListener, ActionListener
    {
        static final int WIDTH = 900, HEIGHT = 600;
        static final Font FONT = new Font("Arial", Font.BOLD, 20);
        static final Color DARKGREEN = new Color(0, 140, 0);
        
        int correct = 0;
        boolean start = false , Q1 = false , Q2 = false, Q3 = false;
        
        JFrame window;
        JMenuBar Menu;
        JMenu startMenu;
       JMenuItem GoAction;
       JRadioButton radButton1;
       JRadioButton radButton2;
       JRadioButton radButton3;
       JRadioButton radButton4;
       JLabel question1;
       JLabel question2;
       JLabel question3;
       JLabel score;
       JButton next1;
       JButton next2;
       JButton finish;
       JCheckBox checkBox1;
       JCheckBox checkBox2;
       JCheckBox checkBox3;
       JCheckBox checkBox4;
       JList listBox;
       
        public static void main(String[] args)
        {
            new SimpleQuiz();
        }
        
        public SimpleQuiz()
        {
            
            
            window = new JFrame();
            
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setLayout(null);
            window.setSize(WIDTH, HEIGHT);
            window.setTitle("Quiz");
            window.getContentPane().setBackground(Color.WHITE);
            window.setLocationRelativeTo(null);
            window.setResizable(false);
            
            //
            //
            //Question 1
            //
            //
            
            radButton1 = new JRadioButton();
            
            radButton1.setFont(new Font("Arial", Font.BOLD, 14));
            radButton1.setForeground(Color.BLACK);
            radButton1.setSize(160, 30);
            radButton1.setText("Los Angeles");
            radButton1.setLocation(300, 180);
            radButton1.setFocusable(false);
            radButton1.setVisible(false);
            window.add(radButton1);
            
            radButton2 = new JRadioButton();
            
            radButton2.setFont(new Font("Arial", Font.BOLD, 14));
            radButton2.setForeground(Color.BLACK);
            radButton2.setSize(160, 30);
            radButton2.setText("Detroit");
            radButton2.setLocation(300, 210);
            radButton2.setFocusable(false);
            radButton2.setVisible(false);
            window.add(radButton2);
            
            radButton3 = new JRadioButton();
            
            radButton3.setFont(new Font("Arial", Font.BOLD, 14));
            radButton3.setForeground(Color.BLACK);
            radButton3.setSize(160, 30);
            radButton3.setText("Shanghai");
            radButton3.setLocation(300, 240);
            radButton3.setFocusable(false);
            radButton3.setVisible(false);
            window.add(radButton3);
            
            radButton4 = new JRadioButton();
            
            radButton4.setFont(new Font("Arial", Font.BOLD, 14));
            radButton4.setForeground(Color.BLACK);
            radButton4.setSize(160, 30);
            radButton4.setText("New York City");
            radButton4.setLocation(300, 270);
            radButton4.setFocusable(false);
            radButton4.setVisible(false);
            window.add(radButton4);
            
            question1 = new JLabel();
            
            question1.setSize(550, 30);
            question1.setLocation(160, 120);
            question1.setFont(new Font("Arial", Font.BOLD, 16));
            question1.setText("Which one of these cities is not located in the United states of America:");
            question1.setOpaque(true);
            question1.setBackground(Color.WHITE);
            question1.setForeground(Color.BLACK);
            question1.setVisible(false);
            window.add(question1);
            
            next1 = new JButton();
    
            next1.setFont(new Font("Arial", Font.BOLD, 28));
            next1.setForeground(Color.BLACK);
            next1.setSize(160, 30);
            next1.setText("NEXT");
            next1.setActionCommand("q2");
            next1.setLocation(700, 500);
            next1.setFocusable(false);
            next1.setVisible(false);
            next1.addActionListener(this);
            window.add(next1);
            
            //
            //
            //Question 2
            //
            //
            
            checkBox1 = new JCheckBox();
            
            checkBox1.setFont(new Font("Arial", Font.BOLD, 14));
            checkBox1.setForeground(Color.BLACK);
            checkBox1.setSize(160, 30);
            checkBox1.setText("13");
            checkBox1.setLocation(300, 180);
            checkBox1.setFocusable(false);
            checkBox1.setVisible(false);
            window.add(checkBox1);
            
            checkBox2 = new JCheckBox();
            
            checkBox2.setFont(new Font("Arial", Font.BOLD, 14));
            checkBox2.setForeground(Color.BLACK);
            checkBox2.setSize(160, 30);
            checkBox2.setText("79");
            checkBox2.setLocation(300, 210);
            checkBox2.setFocusable(false);
            checkBox2.setVisible(false);
            window.add(checkBox2);
            
            checkBox3 = new JCheckBox();
            
            checkBox3.setFont(new Font("Arial", Font.BOLD, 14));
            checkBox3.setForeground(Color.BLACK);
            checkBox3.setSize(160, 30);
            checkBox3.setText("14");
            checkBox3.setLocation(300, 240);
            checkBox3.setFocusable(false);
            checkBox3.setVisible(false);
            window.add(checkBox3);
            
            checkBox4 = new JCheckBox();
            
            checkBox4.setFont(new Font("Arial", Font.BOLD, 14));
            checkBox4.setForeground(Color.BLACK);
            checkBox4.setSize(160, 30);
            checkBox4.setText("87");
            checkBox4.setLocation(300, 270);
            checkBox4.setFocusable(false);
            checkBox4.setVisible(false);
            window.add(checkBox4);
            
            question2 = new JLabel();
            
            question2.setSize(550, 30);
            question2.setLocation(160, 120);
            question2.setFont(new Font("Arial", Font.BOLD, 16));
            question2.setText("Select the prime number(s):");
            question2.setOpaque(true);
            question2.setBackground(Color.WHITE);
            question2.setForeground(Color.BLACK);
            question2.setVisible(false);
            window.add(question2);
            
            next2 = new JButton();
    
            next2.setFont(new Font("Arial", Font.BOLD, 28));
            next2.setForeground(Color.BLACK);
            next2.setSize(160, 30);
            next2.setText("EXT");
            next2.setActionCommand("q3");
            next2.setLocation(700, 500);
            next2.setFocusable(false);
            next2.setVisible(false);
            next2.addActionListener(this);
            window.add(next2);
    
            
            //
            //
            //Question 3
            //
            //
            
            listBox = new JList();
            
            listBox.setFont(new Font("Arial", Font.BOLD, 14));
            listBox.setForeground(Color.BLACK);
            listBox.setSize(160, 30);
            listBox.setLocation(300, 210);
            listBox.setFocusable(false);
            listBox.setVisible(false);
            window.add(listBox);
            
            question3 = new JLabel();
            
            question3.setSize(550, 30);
            question3.setLocation(160, 120);
            question3.setFont(new Font("Arial", Font.BOLD, 16));
            question3.setText("Of the people listed, who was not a US President:");
            question3.setOpaque(true);
            question3.setBackground(Color.WHITE);
            question3.setForeground(Color.BLACK);
            question3.setVisible(false);
            window.add(question3);
            
            finish = new JButton();
    
            finish.setFont(new Font("Arial", Font.BOLD, 28));
            finish.setForeground(Color.BLACK);
            finish.setSize(160, 30);
            finish.setText("FINISH");
            finish.setActionCommand("end");
            finish.setLocation(700, 500);
            finish.setFocusable(false);
            finish.setVisible(false);
            finish.addActionListener(this);
            window.add(finish);
            
            //
            //
            //End
            //
            //
            
            score = new JLabel();
            
            score.setSize(550, 30);
            score.setLocation(160, 120);
            score.setFont(new Font("Arial", Font.BOLD, 16));
            score.setText("your score is: " + correct + "/3");
            score.setOpaque(true);
            score.setBackground(Color.WHITE);
            score.setForeground(Color.BLACK);
            score.setVisible(false);
            window.add(score);
            
            //
            //
            //Extra
            //
            //
            
            
            Menu = new JMenuBar();
            startMenu = new JMenu("Start");
          Menu.add(startMenu);
          
          GoAction = new JMenuItem("Go");
          
          GoAction.setActionCommand("q1");
          GoAction.addActionListener(this);
          startMenu.add(GoAction);
    
            
            //exitMenuItem.addActionListener(this);
    
            
            window.setVisible(true);
    
            window.setJMenuBar(Menu);
            //if (getActionCommand() == "BeginQuiz")
                //System.out.println(Q1);
            
        
        }
        public void keyPressed(KeyEvent e)
        {
        
        }
    
        public void keyReleased(KeyEvent e)
        {
        
        }
    
        public void keyTyped(KeyEvent e)
        {
    
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if (e.getActionCommand().equals("q1"))
            {
                start = true;
                radButton1.setVisible(true);
                radButton2.setVisible(true);
                radButton3.setVisible(true);
                radButton4.setVisible(true);
                question1.setVisible(true);
                next1.setVisible(true);
                System.out.println("Q1");
    
            }
            
            if (e.getActionCommand().equals("q2"))
            {
                {   
                radButton1.setVisible(false);
                radButton2.setVisible(false);
                radButton3.setVisible(false);
                radButton4.setVisible(false);
                question1.setVisible(false);
                next1.setVisible(false);
                System.out.println("Q2");
                
                checkBox1.setVisible(true);
                checkBox2.setVisible(true);
                checkBox3.setVisible(true);
                checkBox4.setVisible(true);
                question2.setVisible(true);
                next2.setVisible(true);
                }
                
                if (e.getActionCommand().equals("q3"))
                {
                    {
                    next2.setVisible(false);    
                    checkBox1.setVisible(false);
                    checkBox2.setVisible(false);
                    checkBox3.setVisible(false);
                    checkBox4.setVisible(false);
                    question2.setVisible(false);
                    System.out.println("Q3");
                    
                    question3.setVisible(true);
                    finish.setVisible(true);
                    }
                    
                    if (e.getActionCommand().equals("end"))
                    {
                        {
                        question3.setVisible(false);
                        finish.setVisible(false);
                        
                        score.setVisible(true);
                        finish.setVisible(true);
            }
        }
    }
    }
    }
    }

As always, thanks for helping me out!

Community
  • 1
  • 1
  • 1
    `window.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 26 '15 at 22:00
  • Please see edit to answer. – Hovercraft Full Of Eels May 27 '15 at 00:30

1 Answers1

5

You've got your if (action command equals q3) if block buried within the previous if block and so it will never be reached when it is in a true state.

e.g. you have something like:

if (e.getActionCommand().equals("q2"))
{
    {   // this block is unnecessary
    // bunch of stuff in here
    }

    //  this block is buried within the if block above, and so will never be true
    if (e.getActionCommand().equals("q3"))
    {
        { // again this block is unnesseary
        // more bunch of code
        }

        // again this block is buried within the previous two!
        if (e.getActionCommand().equals("end"))
        {

To solve the immediate problem, each if block should be at the same block code level and not nested in the prior block.


For example, a simple fix would be to change this:

  if (e.getActionCommand().equals("q2")) {
     {
        radButton1.setVisible(false);
        radButton2.setVisible(false);
        radButton3.setVisible(false);
        radButton4.setVisible(false);
        question1.setVisible(false);
        next1.setVisible(false);
        System.out.println("Q2");

        checkBox1.setVisible(true);
        checkBox2.setVisible(true);
        checkBox3.setVisible(true);
        checkBox4.setVisible(true);
        question2.setVisible(true);
        next2.setVisible(true);
     }

     if (e.getActionCommand().equals("q3")) {
        {
           next2.setVisible(false);
           checkBox1.setVisible(false);
           checkBox2.setVisible(false);
           checkBox3.setVisible(false);
           checkBox4.setVisible(false);
           question2.setVisible(false);
           System.out.println("Q3");

           question3.setVisible(true);
           finish.setVisible(true);
        }

        if (e.getActionCommand().equals("end")) {
           {
              question3.setVisible(false);
              finish.setVisible(false);

              score.setVisible(true);
              finish.setVisible(true);
           }
        }
     }
  }

to this:

public void actionPerformed(ActionEvent e) {
  if (e.getActionCommand().equals("q1")) {
     start = true;
     radButton1.setVisible(true);
     radButton2.setVisible(true);
     radButton3.setVisible(true);
     radButton4.setVisible(true);
     question1.setVisible(true);
     next1.setVisible(true);
     System.out.println("Q1");
  }
  if (e.getActionCommand().equals("q2")) {
     radButton1.setVisible(false);
     radButton2.setVisible(false);
     radButton3.setVisible(false);
     radButton4.setVisible(false);
     question1.setVisible(false);
     next1.setVisible(false);
     System.out.println("Q2");
     checkBox1.setVisible(true);
     checkBox2.setVisible(true);
     checkBox3.setVisible(true);
     checkBox4.setVisible(true);
     question2.setVisible(true);
     next2.setVisible(true);
  }
  if (e.getActionCommand().equals("q3")) {
     next2.setVisible(false);
     checkBox1.setVisible(false);
     checkBox2.setVisible(false);
     checkBox3.setVisible(false);
     checkBox4.setVisible(false);
     question2.setVisible(false);
     System.out.println("Q3");
     question3.setVisible(true);
     finish.setVisible(true);
  }
  if (e.getActionCommand().equals("end")) {
     question3.setVisible(false);
     finish.setVisible(false);
     score.setVisible(true);
     finish.setVisible(true);
  }
}

But more importantly your code is very repetitive and mixes data with code in an unhealthy way. I would first concentrate on creating an OOP-compliant Question class, and only after doing that and testing it, building a GUI around this class. Also rather than swap components, consider swapping the data that the components display. This will make extending and debugging your code much easier.

For example, I'd start with this:

public class Question {
   private String question;
   private String correctAnswer;
   private List<String> wrongAnswers = new ArrayList<>();

   public Question(String question, String correctAnswer) {
      this.question = question;
      this.correctAnswer = correctAnswer;
   }

   public void addWrongAnswer(String wrongAnswer) {
      wrongAnswers.add(wrongAnswer);
   }

   public boolean testAnswer(String possibleAnswer) {
      return correctAnswer.equalsIgnoreCase(possibleAnswer);
   }

   public List<String> getAllRandomAnswers() {
      List<String> allAnswers = new ArrayList<>(wrongAnswers);
      allAnswers.add(correctAnswer);
      Collections.shuffle(allAnswers);
      return allAnswers;
   }

   public String getQuestion() {
      return question;
   }

   public String getCorrectAnswer() {
      return correctAnswer;
   }

   // toString, equals and hashCode need to be done too
}

Then I'd

  • Create a class to hold an ArrayList<Question>, that could give questions as needed, that could tally responses, correct vs. incorrect.
  • Create file I/O routines to store questions in a file, perhaps as a simple text file or better as an XML file or best as a database.
  • Then create a class that creates a JPanel that can display any question and that can get user input.
  • Then a GUI to hold the above JPanel that can get the Question collection from file, that can test the user.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373