首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让我的按钮选择一个随机单词,然后再次选择

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

Stack Overflow用户
提问于 2016-08-23 04:38:43
回答 2查看 94关注 0票数 2

我已经写了一个程序,当按钮被按下一次时,打印出一个随机单词及其定义到文本字段中。我想让它在重复按下按钮时打印另一个随机单词和定义。有什么想法吗?我是编程新手,所以请考虑这一点。

这就是我到目前为止所知道的:

代码语言:javascript
复制
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);

        }
    });
}

}

EN

回答 2

Stack Overflow用户

发布于 2016-08-23 04:41:30

在调用构造函数时,您选择的是随机单词和定义。您必须更改此选项,以便在使用鼠标单击时选择随机单词和定义(在actionPerformed方法中)。

试试这个:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-08-23 04:57:16

尝试删除字符串中的最后一个关键字。Final表示字符串值在声明后不能更改:

代码语言:javascript
复制
   String randomWord = words[idx];

   String randomDef = def[idx];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39088333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档