gpt4 book ai didi

java - 将文本文件中的随机行读取到 JLabel 中

转载 作者:行者123 更新时间:2023-12-01 23:06:32 26 4
gpt4 key购买 nike

我正在创建一个测验,需要我的 JLabel 从文本文件中读取随机行,并将其用作询问用户的问题。我希望问题出现的 JLabel 位于与我的主类不同的 JDialog 类中。

我被告知执行此操作的最佳方法是创建一个文本文件,其中包含所有数据/字符串,然后程序将从该文件中提取信息并将其实现到我的 JLabel 中。

我已经阅读了周围的内容,据我所知,我需要使用缓冲阅读器和文件阅读器,但是,我不完全确定如何将其实现到我的代码中以及如何使其成为每次随机问题。

有人可以帮助我吗,我的 JLabel 代码如下

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

}

最佳答案

为什么不在应用程序启动时读入所有问题,将它们存储在某个集合中,然后在需要时随机获取一个问题?比每次尝试读取文件要容易得多。

关于java - 将文本文件中的随机行读取到 JLabel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22672467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com