简单测验不显示下一个答案

Simple quiz not showing next answer

我正在尝试使用 JFrame 在 Java 中进行“简单”测验。基本上长话短说...当用户在问题 2 后单击“下一步”按钮时,它不会显示下一个问题...

如何获取代码以继续进行问题 3?

代码

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

一如既往,感谢您帮助我!

你的 if (action command equals q3) if 块埋在前一个 if 块中,因此当它处于真实状态时永远不会到达它。

例如你有这样的东西:

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"))
        {

要解决眼前的问题,每个 if 块都应处于同一块代码级别,而不是嵌套在前一个块中。


例如,一个简单的修复方法是更改​​此:

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

对此:

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

但更重要的是,您的代码非常重复,并且以不健康的方式将数据与代码混合在一起。我会首先专注于创建一个符合 OOP 的问题 class,只有在这样做并对其进行测试之后,才能围绕此 class 构建一个 GUI。此外,与其交换组件,不如考虑交换组件显示的数据。这将使扩展和调试您的代码 更容易。

例如,我会这样开始:

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
}

然后我

  • 创建一个 class 来容纳一个 ArrayList<Question>,可以根据需要提出问题,可以统计回答,正确与错误。
  • 创建文件 I/O 例程以将问题存储在文件中,可能作为简单的文本文件或更好地作为 XML 文件或最好作为数据库。
  • 然后创建一个 class 来创建一个可以显示任何问题并可以获取用户输入的 JPanel。
  • 然后是一个 GUI 来保存上面的 JPanel,它可以从文件中获取问题集合,可以测试用户。