java - 在读取文本文件的某些部分之前,让 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
解决方案
推荐阅读
- sql - 聚合和团体预订
- powershell - LastWriteTime 未排序为 DateTime
- r - r:使用 lm 和 ggplot2 生成拟合图与实际图
- c# - 调用目标(MethodBase.Invoke 方法)抛出了错误异常
- java - 需要我的二维数组来打印,但它只在输出框中显示一个点
- php - 如何获取 ajax 响应值/字符串并在 if 条件下使用它?
- javascript - 在给定级别汇总树(嵌套对象)
- java - 间歇性 Jackson 模型映射器参考链错误
- for-loop - 访问 VBA 嵌套 for 循环仅适用于第一条记录
- c# - Display information on screen during process