我正在创建一个游戏,我正在尝试使用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);
这是不同类别的问题的代码
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类中的显示方式
enter code here displayQuestion();
displayAnswer1();
displayAnswer2();
displayAnswer4();
//This code will display the question and answers
}
这是GUI类中问题代码的下一部分
enter code here public void displayQuestion()
{
QuestionDetails q = questHandler.generateResponse();
String question = q.getQuestion();
textAreaQuestion.setText(question);
//This will display the array of questions
}
此致
答案 0 :(得分:0)
如果textAreaQuestion是JTextArea
你必须做textAreaQuestion.getText()
这样:
if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?"))
答案 1 :(得分:0)
它无法工作的原因是因为数组已被用于答案。在这种情况下,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{
}