我的程序如何随机按下按钮?
How can my program randomly press a button?
我正在尝试制作一个按钮会亮起并且用户必须在给定时间内按下按钮的游戏。
目前,我的程序有 12 个按钮可以执行某些操作。我正在尝试让程序随机调用这些按钮。到目前为止,我只有 12 个按钮,当用户按下时,这些按钮只会更改文本。
现在我需要一种方法来让它们随机按下程序本身而不是用户。关于如何在 java 中完成此操作有任何想法吗?
// **** Panels for buttons ****
JPanel panelButtons = new JPanel(); // making the panel for the buttons
panelButtons.setLayout(new GridLayout(3, 4)); // setting the layout of the buttons to 3x4 as shown above
b1 = new JButton(" ⃝"); // creating button and setting its default text
b1.setFont(fontText); // setting the font
b1.addActionListener(new ActionListener(){ // action listener to do something when pressed
public void actionPerformed(ActionEvent e) {
sendMessage(user + "1" ); // sends the name of the user that pressed the button and which button
String field1 = b1.getText(); // gets the text from the button and stores it in a String
if(field1 == " ⃝"){ // checks if the string is equal to an empty circle
b1.setText("⬤"); // if true then change to a full circle
}
else if (field1 == "⬤"){ // opposite of the above if statement
b1.setText(" ⃝");
}
}
});
panelButtons.add(b1); // adding the button to the panel
b2 = new JButton(" ⃝"); // creating button and setting its default text
b2.setFont(fontText); // setting the font
b2.addActionListener(new ActionListener(){ // action listener to do something when pressed
public void actionPerformed(ActionEvent e) {
sendMessage(user + "2" ); // sends the name of the user that pressed the button and which button
String field2 = b2.getText(); // gets the text from the button and stores it in a String
if(field2 == " ⃝"){ // checks if the string is equal to an empty circle
b2.setText("⬤"); // if true then change to a full circle
}
else if (field2 == "⬤"){ // opposite of the above if statement
b2.setText(" ⃝");
}
}
});
panelButtons.add(b2); // adding the button to the panel
您可以创建一个包含按钮的列表。使用随机数生成器在列表长度内创建一个随机数。使用该(随机)索引修改相应的按钮。
- 将您的十二个按钮放入有序集合中。
- 将您的十二个相应操作以
Consumer<JButton>'(use
Callable` 的形式放入另一个有序集合中(并忽略 return),或者如果您不使用 [=34=,则创建类似的东西] 8).
- 执行从 Button 集合到 action 集合的映射。
像这样创建一个 class 实现 ActionListener
:
private static class Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
button2action.get((JButton)e.getSource()).accept(a);
}
}
如果您想按下随机按钮,请从地图的值集中选取一个随机元素。
int randomIndex = new random().nextInt(button2action.entrySet().size());
Iterator<Entry<JButton, Consumer<JButton>>> iter = button2action.entrySet().iterator();
for (int i = 0; i < randomIndex; i++){
Entry<JButton, Consumer<JButton>> entry = iter.next();
}
entry.getValue().accept(entry.getKey());
如果要添加新按钮,请将新按钮操作对添加到地图。
我正在尝试制作一个按钮会亮起并且用户必须在给定时间内按下按钮的游戏。
目前,我的程序有 12 个按钮可以执行某些操作。我正在尝试让程序随机调用这些按钮。到目前为止,我只有 12 个按钮,当用户按下时,这些按钮只会更改文本。
现在我需要一种方法来让它们随机按下程序本身而不是用户。关于如何在 java 中完成此操作有任何想法吗?
// **** Panels for buttons ****
JPanel panelButtons = new JPanel(); // making the panel for the buttons
panelButtons.setLayout(new GridLayout(3, 4)); // setting the layout of the buttons to 3x4 as shown above
b1 = new JButton(" ⃝"); // creating button and setting its default text
b1.setFont(fontText); // setting the font
b1.addActionListener(new ActionListener(){ // action listener to do something when pressed
public void actionPerformed(ActionEvent e) {
sendMessage(user + "1" ); // sends the name of the user that pressed the button and which button
String field1 = b1.getText(); // gets the text from the button and stores it in a String
if(field1 == " ⃝"){ // checks if the string is equal to an empty circle
b1.setText("⬤"); // if true then change to a full circle
}
else if (field1 == "⬤"){ // opposite of the above if statement
b1.setText(" ⃝");
}
}
});
panelButtons.add(b1); // adding the button to the panel
b2 = new JButton(" ⃝"); // creating button and setting its default text
b2.setFont(fontText); // setting the font
b2.addActionListener(new ActionListener(){ // action listener to do something when pressed
public void actionPerformed(ActionEvent e) {
sendMessage(user + "2" ); // sends the name of the user that pressed the button and which button
String field2 = b2.getText(); // gets the text from the button and stores it in a String
if(field2 == " ⃝"){ // checks if the string is equal to an empty circle
b2.setText("⬤"); // if true then change to a full circle
}
else if (field2 == "⬤"){ // opposite of the above if statement
b2.setText(" ⃝");
}
}
});
panelButtons.add(b2); // adding the button to the panel
您可以创建一个包含按钮的列表。使用随机数生成器在列表长度内创建一个随机数。使用该(随机)索引修改相应的按钮。
- 将您的十二个按钮放入有序集合中。
- 将您的十二个相应操作以
Consumer<JButton>'(use
Callable` 的形式放入另一个有序集合中(并忽略 return),或者如果您不使用 [=34=,则创建类似的东西] 8). - 执行从 Button 集合到 action 集合的映射。
像这样创建一个 class 实现
ActionListener
:private static class Listener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { button2action.get((JButton)e.getSource()).accept(a); } }
如果您想按下随机按钮,请从地图的值集中选取一个随机元素。
int randomIndex = new random().nextInt(button2action.entrySet().size()); Iterator<Entry<JButton, Consumer<JButton>>> iter = button2action.entrySet().iterator(); for (int i = 0; i < randomIndex; i++){ Entry<JButton, Consumer<JButton>> entry = iter.next(); } entry.getValue().accept(entry.getKey());
如果要添加新按钮,请将新按钮操作对添加到地图。