简体   繁体   中英

Reading random lines from a text file into a JLabel

I am creating a quiz and need my JLabel to read a random line from a text file and use this as the question to ask the user. The JLabel I want the question to appear on are in a separate JDialog class from my main class.

I have been told the best way to do this, is to create a text file which contains all of the data/strings which the program will then pull information from and implement it into my JLabel .

I have read around and from what I am aware I need to use a buffered reader and also a file reader, however, I am not entirely sure how to implement this into my code and also how to make it so that it is a random question each time.

Could somebody please help me, my code for the JLabel is as follows

package ZillionaireGUI;

import java.awt.Frame;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;


public class questionDialog extends JDialog {
private JLabel Question;
private JRadioButton answerThree;
private JRadioButton answerFour;
private JRadioButton answerTwo;
private JRadioButton answerOne;
public questionDialog(Frame parent) {
    super(parent);
}


public questionDialog(JFrame frame) {
    super(frame);
    initGUI();
}

private void initGUI() {
    try {

        getContentPane().setLayout(null);

        Question = new JLabel();
        getContentPane().add(Question);
        Question.setText("jLabel1");
        Question.setBounds(39, 127, 383, 29);

        answerOne = new JRadioButton();
        getContentPane().add(answerOne);
        answerOne.setText("jRadioButton1");
        answerOne.setBounds(26, 183, 93, 20);

        answerTwo = new JRadioButton();
        getContentPane().add(answerTwo);
        answerTwo.setText("jRadioButton1");
        answerTwo.setBounds(130, 183, 93, 20);

        answerThree = new JRadioButton();
        getContentPane().add(answerThree);
        answerThree.setText("jRadioButton1");
        answerThree.setBounds(247, 183, 93, 20);

        answerFour = new JRadioButton();
        getContentPane().add(answerFour);
        answerFour.setText("jRadioButton1");
        answerFour.setBounds(360, 183, 93, 20);

        ButtonGroup group = new ButtonGroup();
        group.add(answerOne);
        group.add(answerTwo);
        group.add(answerThree);
        group.add(answerFour);


        this.setSize(490, 393);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Why not read in all the questions when the application starts, store them in some collection, and then just grab a random one when you need it? Much easier than trying to read the file each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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