简体   繁体   English

按下按钮时读取一行

[英]Read a line when button pressed

I have to do a quiz class that will read the questions from a text file, I am stuck trying to make the program work in such way that every time the answer button is pressed it reads the next question (line in the text file), I tried many suggestions from the internet but the furthest i got was to read the first line when the program runs then using String[] args = new String[0]; 我必须做一个测验类,以从文本文件中读取问题,我一直试图使程序正常工作,每次按下答案按钮时它都会读取下一个问题(文本文件中的行),我尝试了很多来自互联网的建议,但是我得到的最远的是在程序运行时阅读第一行,然后使用String[] args = new String[0]; to have a new frame when the button is pressed but then the text wouldn't read at all. 按下按钮时会有一个新的框,但是文本根本不会读。

Here is my code: 这是我的代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Frame {

    JFrame frame;
    JPanel pan1;
    static JButton boutonRetour,boutonEnregistrer;
    static JTextField reponse,question;
    static int counter1 = 1;
    static int lines=0;


    public static void main(String[] args) {

        final JFrame frame = new JFrame("Frame");

        frame.setSize(700,600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true);
        question=new JTextField(10);
        question.setBounds(150, 220, 450, 50); 
        question.setEditable(true);
        frame.setContentPane(new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(null, 0, 0, 700, 600, this);
            }
        });

        boutonEnregistrer=new JButton("Answer");
        boutonEnregistrer.setBounds(0,513,100,50);

        boutonEnregistrer.setBackground(new Color(0, 0, 182));
        boutonEnregistrer.setForeground(Color.white);
        boutonEnregistrer.setFocusPainted(false);
        boutonEnregistrer.setFont(new Font("Tahoma", Font.BOLD, 12));

        boutonEnregistrer.addActionListener(new ActionListener(){ 

            public void actionPerformed (ActionEvent e){
                counter1++;  
                String[] args = new String[0];              

                main(args); 
                /*
                SwingUtilities.updateComponentTreeUI(frame);
                frame.revalidate();
                frame.repaint();
                frame.removeAll();
                frame.invalidate();
                frame.validate();
                */   
            } 
        });


        frame.add(question);        
        frame.add(boutonEnregistrer);
        frame.setLayout(null);
        System.out.println(counter1);


        try {        
            BufferedReader br =
            new BufferedReader(new FileReader("Questions.txt"));
            String word = null;
            while((word =br.readLine()) != null) {
                lines++;
            if (lines == counter1){
                question.setText(word);
                break;

} }

            }

        } catch(IOException err) {

        }                          
        frame.validate();
        System.out.println(lines);

    };
}

You use the lines variable to keep track of the last line that was read. 您可以使用lines变量来跟踪最后读取的行。 However that is not happening in your code. 但是,这不会在您的代码中发生。 The while loop always runs until the reader reaches EOF. while循环始终运行,直到读取器到达EOF。

You should break from the loop once you read the desired line: 阅读所需的行后,您应该退出循环:

if (lines == counter1){
    question.setText(word);
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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