java - 我的程序如何随机按下按钮?

标签 java button random jframe

我正在尝试制作一款游戏,其中按钮会亮起,并且用户必须在给定时间内按下按钮。

目前,我的程序有 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

最佳答案

您可以创建一个包含按钮的列表。使用随机数生成器在列表长度内创建一个随机数。使用该(随机)索引来修改相应的按钮。

关于java - 我的程序如何随机按下按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29253414/

相关文章:

javascript - 如何使用相同的弹出按钮在新选项卡中打开另一个链接

java - 生成包含字母数字和特殊字符的随机字符串?

java - 如何从 firebase 实时数据库检索数据并将其显示在个人资料仪表板中

apache-flex - Flex 4 - 单元格中带有按钮的 DataGrid

android - AlertDialog 按钮总是在单击后关闭对话框

java - 返回随机 boolean 值并用于引导 "if"语句

azure - 有没有办法使用 Terraform 中的一个 `random_uuid` 资源生成一组随机 UUID

java - 如何在 web.xml 中配置 spring-boot servlet?

java - 如何正确地将TextureRegion映射到Pixmap?

Java Swing getSize() 返回不准确的值?