2016-08-22 1 views
2

ボタンを一度押すとランダムな単語とその定義をテキストフィールドに出力するプログラムを作成しました。ボタンを繰り返し押すと、別のランダムな単語と定義を印刷するようにしたいと思います。何か案は? 私は新しいコーディングですので、考慮してください。私のボタンをランダムな単語を選択する方法とその後に戻る

これは私がこれまで持っているものです。

public class RandomWord extends JFrame{ 
private JTextField wordField; 
private JTextField defField; 
private JButton nextButton; 
private String words[] = {"Petrichor:",//0 
         "Iterate:",//1 
         "Absquatulate:",//2 
         "Anhuiliform:",//3 
         "Argle-bargle:","Argus-eyed:",//4 
         "Automy:",//5 
         "Benthos:",//6 
         "Bibliopole:",//7 
         "Bilboes:",//8 
         "Bruxism:",//9 
         "Borborygmus:",//10 
         "Calipygian:",//11 
         "Callithumpian:",//12 
         "Cereology:",//13 
         "Chad:",//14 
         "Chiliad:"};//15 

private String def[] = {"The smell of earth after rain.",//0 
         "To utter or perform repeatedly.",//1 
         "To leave somewhere abruptly.",//2 
         "Resembling an eel.",//3 
         "Copious but meaningless talk or writing.",//4 
         "Vigilant, refering to Argos a Greek mythological watchman with a hundred eyes.",//5 
         "The casting off of a limb or other part of the body by an animal under threat, such as a lizard.",//6 
         "The flora and faunda on the bottom of a sea or lake.",//7 
         "A person who buys and sells books, especially rare ones",//8 
         "An iron bar with sliding shackles, used to fasten prisoners' ankles.",//9 
         "Involantary and habitual grinding of the teeth.",//10 
         "A rumbling or gurgling noise in the intestines.",//11 
         "Having shapely buttocks.","Like a discordant band or a noisy parade.",//12 
         "The study or investigation of crop circles.",//13 
         "A piece of waste paper produced by punching a hole.",//14 
         "A thousand things or a thousand years."};//15 

public RandomWord(){ 
    super("Cool Words -1.5"); 
    setLayout(new FlowLayout()); 

    int idx = new Random().nextInt(words.length); 
    final String randomWord = words[idx]; 
    final String randomDef = def[idx]; 

    wordField = new JTextField("Petrichor",20); 
    add(wordField); 
    defField = new JTextField("The smell of earth after rain",20); 
    add(defField); 
    nextButton = new JButton("Next"); 
    add(nextButton); 

    nextButton.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent arg0) { 
      wordField.setText(randomWord); 
      defField.setText(randomDef); 

     } 
    }); 
} 

}

+0

ランダムなインデックス番号を生成するときにボタンをクリックすると、最終的には表示されません。 –

+1

それは簡単でした。ハッハッハー、私はばかみたいだね!感謝万円!魅力のように働く。 – CapnCoin

答えて

0

あなたが呼ばれたコンストラクタでランダムな単語と定義を選んでいます。 マウスでクリックするときにランダムな単語と定義を選択するには、これを変更する必要があります(actionPerformedメソッド内)。

はこれで試してみてください:

public RandomWord(){ 
    super("Cool Words -1.5"); 
    setLayout(new FlowLayout()); 

    wordField = new JTextField("Petrichor",20); 
    add(wordField); 
    defField = new JTextField("The smell of earth after rain",20); 
    add(defField); 
    nextButton = new JButton("Next"); 
    add(nextButton); 

    nextButton.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent arg0) { 

      // this method is executed when you clicked; 
      // The random word and definition must be choose at this moment. 
      int idx = new Random().nextInt(words.length); 
      wordField.setText(words[idx];); 
      defField.setText(def[idx]); 

     } 
    }); 
} 
+0

コードダンプの質問は適切ではありませんが、どちらもコードダンプの回答ではありません。サイトとOPは、スプーンフィードコードのダンプではなく、適切な説明からはるかに多くの利益を得ます。 –

+0

魅力的な作品です。何が起こっているのかを正確に把握するためにコードを実行していたので、ランダムな配列要素を文字列に取り込み、それを差し込むことが冗長であることに気づいた。 – CapnCoin

+0

@HovercraftFullOfEels申し訳ありません。 –

0

は、あなたの文字列の最後のキーワードを削除してください。 finalは、宣言後にString値を変更できないことを示します。

String randomWord = words[idx]; 

    String randomDef = def[idx]; 
関連する問題