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

Java 不显示下一个答案的简单测验,java,swing,jframe,Java,Swing,Jframe,我正在尝试使用JFrame用Java做一个“简单”的测验。基本上是长话短说。。。当用户单击问题2后的“下一步”按钮时,不会显示下一个问题 如何获取代码以继续问题3 代码 一如既往,谢谢你帮助我 您的if(action命令等于q3)if块埋在前一个if块中,因此当它处于真实状态时将永远无法到达 e、 g.你有类似于: if (e.getActionCommand().equals("q2")) { { // this block is unnecessary // bunch

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

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

代码 一如既往,谢谢你帮助我

您的
if(action命令等于q3)
if块埋在前一个if块中,因此当它处于真实状态时将永远无法到达

e、 g.你有类似于:

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兼容的问题类,只有在这样做并进行测试之后,才能围绕这个类构建一个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
}
公开课问题{
私有字符串问题;
私人字符串更正应答;
私有列表错误答案=新建ArrayList();
公开问题(字符串问题、字符串答案){
这个问题=问题;
this.correctAnswer=correctAnswer;
}
public void addErrorAnswer(字符串错误答案){
错误答案。添加(错误答案);
}
公共布尔测试拒绝(字符串可能拒绝){
返回正确答案。相等信号情况(可能的回答);
}
公共列表getAllRandomAnswers(){
列出allAnswers=新的ArrayList(错误答案);
allAnswers.add(正确答案);
收藏。洗牌(allAnswers);
返回allAnswers;
}
公共字符串getQuestion(){
返回问题;
}
公共字符串getCorrectAnswer(){
返回正确答案;
}
//toString、equals和hashCode也需要完成
}
那我就

  • 创建一个类来保存一个
    ArrayList
    ,它可以根据需要给出问题,可以记录回答,正确与不正确
  • 创建文件I/O例程以将问题存储在文件中,可以是简单的文本文件,更好的是XML文件,最好是数据库
  • 然后创建一个类,该类创建一个JPanel,可以显示任何问题并获取用户输入
  • 然后一个GUI来保存上面的JPanel,它可以从文件中获取问题集合,可以测试用户

window.setLayout(空)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。改用布局管理器,或与布局填充和边框一起使用。请参阅编辑以回答。
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
}