2016-04-09 7 views
0

ゲームを作成していて、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"); 
      } 
     } 
    } 
} 

を使用しようとしています方法である誰かが私に教えてくださいすることができ私のコード。ここで

は、これは別のクラスにある質問のためのコードである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 

} 

よろしく、ケースtextAreaQuestionで

+0

はtextAreaQuestion文字列ですか? – Dimi

+0

'' textAreaQuestion''のタイプは? – muzahidbechara

+0

これはプライベートJTextArea textAreaQuestionとして宣言されています。 –

答えて

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{ 
        } 
0

は、あなたがそうtextAreaQuestion.getText()

しなければならないJTextAreaです:

if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")) 
+0

申し訳ありません。ボタンを空にしておくだけです。 –

+0

あなたはあなたの質問を編集し、 'textAreaQuestion'を設定しているコードを投稿できますか? – Dimi