I am creating a game and I am trying to display an answer in a JButton using the setText method. This is what I tried to do with the button
enter code here btnAnswer3 = new JButton(setQuestion);
This is the method I am trying to use
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");
}
}
}
}
Can someone please tell me why this is not working and how it should be done using my code.
Here is the code for the textAreaQuestion
enter code here textAreaQuestion = new JTextArea();
textAreaQuestion.setEditable(false);
questions.setViewportView(textAreaQuestion);
This is the code for the questions which is in a different 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);
}
This is how it is displayed in the GUI class
enter code here displayQuestion();
displayAnswer1();
displayAnswer2();
displayAnswer4();
//This code will display the question and answers
}
This is the next part of the questions code in the 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
}
Regards,
The reason why it is not working is because an array has been used for the answers. In this case the setText method will not be of any use.
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);}
The way to fix this you would need to remove the answers from the Array like this
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?")); }
Then this method would work effectively.
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{
}