Java 如何让我的按钮选择一个随机的词,然后再次
我编写了一个程序,当按下按钮一次时,将一个随机单词及其定义打印到文本字段中。我想让它在反复按下按钮时打印另一个随机单词和定义。有什么想法吗? 我是新的编码,所以请考虑到这一点 这就是我到目前为止所做的:Java 如何让我的按钮选择一个随机的词,然后再次,java,arrays,int,element,elements,Java,Arrays,Int,Element,Elements,我编写了一个程序,当按下按钮一次时,将一个随机单词及其定义打印到文本字段中。我想让它在反复按下按钮时打印另一个随机单词和定义。有什么想法吗? 我是新的编码,所以请考虑到这一点 这就是我到目前为止所做的: public class RandomWord extends JFrame{ private JTextField wordField; private JTextField defField; private JButton nextButton; private String words[
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);
}
});
}
}您正在选择随机词,并在调用构造函数时定义。 当用鼠标单击时,必须更改此选项以选择随机单词和定义(在
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]);
}
});
}
尝试删除字符串中的最后一个关键字。Final表示字符串值在声明后不能更改:
String randomWord = words[idx];
String randomDef = def[idx];
不要让它成为最终结果,当他们点击按钮时,应该是你生成随机索引号的时候。这很简单。哈哈,我觉得自己像个白痴!万分感谢!代码转储问题不合适,但代码转储答案也不合适。该网站和OP将从适当的解释中受益匪浅,而不是一匙羹式的代码转储。当我在查看代码以试图准确了解发生了什么时,我注意到将随机数组元素转换为字符串,然后将其插入是多余的:)@CapnCoin编辑了一个解释,如果不够清楚,请告诉我。