在 JButton 中显示 if 语句 setText
Display if statement setText in a JButton
我正在创建游戏,并尝试使用 setText 方法在 JButton 中显示答案。这就是我尝试用按钮做的
enter code here btnAnswer3 = new JButton(setQuestion);
这是我正在尝试使用的方法
enter code here public void setQuestion()
{
if (textAreaQuestion.equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer3.setText("C. Harold Wilson");
} else{
if (textAreaQuestion.equals("Where did Bruce Lee open his first Martial Arts School?")){
btnAnswer3.setText("C. Seattle");
} else{
if (textAreaQuestion.equals("Who was the Prime Minister of England in 1940?")){
btnAnswer3.setText("C. Winston Churchill");
}
}
}
}
有人可以告诉我为什么这不起作用以及如何使用我的代码完成它。
这是 textAreaQuestion 的代码
enter code here textAreaQuestion = new JTextArea();
textAreaQuestion.setEditable(false);
questions.setViewportView(textAreaQuestion);
这是另一个 class
中的问题代码
enter code here private ArrayList<QuestionDetails> Questions = new ArrayList<QuestionDetails>();
public Questions()
{
Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?","Winston Churchill","Tony Blair","Harold Macmillon"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?","John Kennedy","Harold Wilson","Harold Macmillon"));
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?","Baltimore","Hong Kong","Hollywood"));
}
public QuestionDetails generateResponse()
{
Random r = new Random();
int index = r.nextInt(Questions.size());
return Questions.get(index);
}
这是它在 GUI 中的显示方式 class
enter code here displayQuestion();
displayAnswer1();
displayAnswer2();
displayAnswer4();
//This code will display the question and answers
}
这是 GUI 中问题代码的下一部分 class
enter code here public void displayQuestion()
{
QuestionDetails q = questHandler.generateResponse();
String question = q.getQuestion();
textAreaQuestion.setText(question);
//This will display the array of questions
}
此致,
如果 textAreaQuestion 是 JTextArea
你必须做 textAreaQuestion.getText()
所以:
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?"))
它不起作用的原因是因为数组已用于答案。在这种情况下,setText 方法将没有任何用处。
Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?","Winston Churchill","Tony Blair","Harold Macmillon"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?","John Kennedy","Harold Wilson","Harold Macmillon"));
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?","Baltimore","Hong Kong","Hollywood"));}
public QuestionDetails generateResponse(){ Random r = new Random();
int index = r.nextInt(Questions.size());
return Questions.get(index);}
解决这个问题的方法是像这样从数组中删除答案
enter code here Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?"));
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?")); }
那么这个方法就有效了
enter code here if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer3.setText("C."+" Harold Wilson");
}
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer1.setText("A."+" Tony Blair");
}
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer2.setText("B."+" Harold Wilson");
}
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer4.setText("D."+" Harold Wilson");
} else{
}
我正在创建游戏,并尝试使用 setText 方法在 JButton 中显示答案。这就是我尝试用按钮做的
enter code here btnAnswer3 = new JButton(setQuestion);
这是我正在尝试使用的方法
enter code here public void setQuestion()
{
if (textAreaQuestion.equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer3.setText("C. Harold Wilson");
} else{
if (textAreaQuestion.equals("Where did Bruce Lee open his first Martial Arts School?")){
btnAnswer3.setText("C. Seattle");
} else{
if (textAreaQuestion.equals("Who was the Prime Minister of England in 1940?")){
btnAnswer3.setText("C. Winston Churchill");
}
}
}
}
有人可以告诉我为什么这不起作用以及如何使用我的代码完成它。
这是 textAreaQuestion 的代码
enter code here textAreaQuestion = new JTextArea();
textAreaQuestion.setEditable(false);
questions.setViewportView(textAreaQuestion);
这是另一个 class
中的问题代码enter code here private ArrayList<QuestionDetails> Questions = new ArrayList<QuestionDetails>();
public Questions()
{
Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?","Winston Churchill","Tony Blair","Harold Macmillon"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?","John Kennedy","Harold Wilson","Harold Macmillon"));
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?","Baltimore","Hong Kong","Hollywood"));
}
public QuestionDetails generateResponse()
{
Random r = new Random();
int index = r.nextInt(Questions.size());
return Questions.get(index);
}
这是它在 GUI 中的显示方式 class
enter code here displayQuestion();
displayAnswer1();
displayAnswer2();
displayAnswer4();
//This code will display the question and answers
}
这是 GUI 中问题代码的下一部分 class
enter code here public void displayQuestion()
{
QuestionDetails q = questHandler.generateResponse();
String question = q.getQuestion();
textAreaQuestion.setText(question);
//This will display the array of questions
}
此致,
如果 textAreaQuestion 是 JTextArea
你必须做 textAreaQuestion.getText()
所以:
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?"))
它不起作用的原因是因为数组已用于答案。在这种情况下,setText 方法将没有任何用处。
Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?","Winston Churchill","Tony Blair","Harold Macmillon"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?","John Kennedy","Harold Wilson","Harold Macmillon"));
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?","Baltimore","Hong Kong","Hollywood"));}
public QuestionDetails generateResponse(){ Random r = new Random();
int index = r.nextInt(Questions.size());
return Questions.get(index);}
解决这个问题的方法是像这样从数组中删除答案
enter code here Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?"));
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?")); }
那么这个方法就有效了
enter code here if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer3.setText("C."+" Harold Wilson");
}
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer1.setText("A."+" Tony Blair");
}
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer2.setText("B."+" Harold Wilson");
}
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
btnAnswer4.setText("D."+" Harold Wilson");
} else{
}