首页 > 解决方案 > 在读取文本文件的某些部分之前,让 Java Netbeans GUI 构建器在事件之前等待

问题描述

我目前正在制作一个谁想成为百万富翁的游戏,我对 GUI 很陌生,但我从文本文件中阅读了我的问题和选项,我以前在 CLI 上制作过同样的游戏,我使用扫描仪类制作系统在继续下一个问题之前等待输入,但我不太确定如何使用 GUI 做同样的事情,它会不停地读取所有问题。有没有办法让它在问题之间等待?或任何更好的选择?我不想硬编码问题。

这是做问题阅读部分的主要代码

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        String answer = null;
        String question = null;
        String a = null;
        String b = null;
        String c = null;
        String d = null;
        
        InputOutput getIO = new InputOutput();
        getIO.readQuestions();
        
         for(int counter = 0; counter < 15; counter++)
         { 
          // int q = 0;
            question = getIO.readQuestion(); // Read the question
            a = getIO.readA(); // Read option a
            b = getIO.readB(); // Read option b
            c = getIO.readC(); // Read option c
            d = getIO.readD(); // Read option d
            
           // System.out.println(a);
            // Stored the correct answer from the file for later usage
            answer = getIO.readAnswer();
            
            jTextField1.setText(question);
            jTextField2.setText(a);
            jTextField3.setText(b);
            jTextField4.setText(c);
            jTextField5.setText(d);
            
        }
        
    }                                     

这是 InputOutput 类

 @Override
     public void readQuestions() // Method to read the text file
    {
        try
        {
            in = new FileReader("./resources/Questions.txt"); 
            read = new BufferedReader(in);
        }
        catch(FileNotFoundException e){
            System.out.println("File not found");
        }   
    }

   @Override
   public String readQuestion() // Read the first line of the question
    {
        try
        {
           getQuestion  = read.readLine();
          // System.out.println(getQuestion);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getQuestion;
    }
   
    @Override
   public String readA() // Read option 'a'
   { 
        try
        {
           getA  = read.readLine();
         //  System.out.println(getA);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getA;
   }
   
    @Override
   public String readB() // Read option 'b'
   { 
        try
        {
           getB  = read.readLine();
           //System.out.println(getB);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getB;
   }
   
    @Override
   public String readC() //Read option 'c'
   { 
        try
        {
           getC  = read.readLine();
           //System.out.println(getC);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getC;
   }
   
    @Override
   public String readD() // Read option 'd'
   { 
        try
        {
           getD  = read.readLine();
           //System.out.println(getD);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getD;
   }
   
    @Override
   public String readAnswer() // Read answer
   { 
        try
        {
           getAnswer  = read.readLine();
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getAnswer;
   }

这是文本文件的排列方式

What does 'NFL' stand for
a) National Food League
b) National Federation League
c) National Football League
d) National Fighting League
c
what is 9+2*5
a) 55
b) 60
c) 19
d) 40
c

标签: javauser-interfacenetbeans

解决方案


推荐阅读