用户界面使Java Netbeans GUI生成器在读取文本文件的某些部分之前等待事件发生
我目前正在制作一个《谁想成为百万富翁》游戏,我对GUI非常陌生,但我从文本文件中阅读了我的问题和选项,我以前在CLI上制作过同一个游戏,我使用scanner类让系统在进入下一个问题之前等待输入,但我不太确定如何使用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
共 (0) 个答案