java - 按下按钮时读取一行

标签 java swing file-io jbutton bufferedreader

我必须做一个测验类(class),从文本文件中读取问题,我一直试图让程序以这样的方式工作:每次按下答案按钮时,它都会读取下一个问题(文本中的行)文件),我尝试了互联网上的许多建议,但我得到的最远的是在程序运行时读取第一行,然后使用 String[] args = new String[0]; 获得一个新框架当按下按钮但随后文本根本无法读取。

这是我的代码:

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

    };
}

最佳答案

您使用lines变量来跟踪读取的最后一行。但是,这在您的代码中没有发生。 while 循环始终运行,直到读取器到达 EOF。

一旦读到所需的行,您就应该从循环中中断:

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

关于java - 按下按钮时读取一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22971697/

相关文章:

java - "Catch"一种方法的多个按钮

java - 从 YANG 数据模型生成 XML RPC NETCONF 请求的标准方法是什么

java - 我可以从 System.nanoTime 合理预期的最差分辨率是多少?

c++ - 多进程打开同一个文件导致文件操作失败

c# - 有没有更好的方法来覆盖文件然后检查更改(在 C# 中)?

java - 哈希集如何发生冲突?

java - 使用 JPanel/JFrame 的 2D 射击游戏。以自己的速度在屏幕上绘制图像(子弹)

java - JTable - 对渲染值进行排序

java - 为什么需要在 JLabel 中重置 setText() 以防止错误?

python - 尽管出现 UnicodeDecodeError 错误,我如何继续处理文件?