简体   繁体   English

如何让我的按钮选择一个随机单词,然后再

[英]how to make my button pick a random word and then after again

I've written a program to print out a random word and its definition to a text field when the button is pressed once. 我编写了一个程序,可以在按下按钮时将随机单词及其定义打印到文本字段。 I want to have it to print another random word and definition when The button is pressed repeatedly. 反复按下按钮时,我想让它打印另一个随机的单词和定义。 Any Ideas? 有任何想法吗? I am new to coding so please take that into consideration. 我是编码新手,因此请考虑到这一点。

This is what I have so far: 这是我到目前为止的内容:

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);

        }
    });
}

} }

You are choosing the random word and definition at the constructor was called. 您正在选择随机单词,并在构造函数处调用了定义。 You must to change this to choose the random word and definition when you are click with mouse (inside actionPerformed method). 当您用鼠标单击时(在actionPerformed方法内部),必须更改此项以选择随机词和定义。

Try with this: 试试这个:

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]);

        }
    });
}

Try removing the final keyword in your Strings. 尝试删除字符串中的final关键字。 Final denotes that the String value cannot be changed after its declaration: Final表示String值在声明后不能更改:

   String randomWord = words[idx];

   String randomDef = def[idx];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM