ゲームを作成していて、setTextメソッドを使用してJButtonに回答を表示しようとしています。これが動作し、どのようにそれを使用して行われる必要がありますされていない理由は、私がボタンifステートメントを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");
}
}
}
}
を使用しようとしています方法である誰かが私に教えてくださいすることができ私のコード。ここで
は、これは別のクラスにある質問のためのコードであるtextAreaQuestionenter 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
}
よろしく、ケースtextAreaQuestionで
はtextAreaQuestion文字列ですか? – Dimi
'' textAreaQuestion''のタイプは? – muzahidbechara
これはプライベートJTextArea textAreaQuestionとして宣言されています。 –