Trouble having 2D Array of buttons click themselves randomly using the computer and not user inputs [duplicate]

跟風遠走 提交于 2020-01-22 03:44:25

问题


I'm currently making a 2D array of buttons (3x3) and need some help having the program click a random button by itself. I'm currently attempting to use doClick() without any success. I need to be able to have the program click a random button all by itself from my 3x3 buttons.

this is how I created my buttons

for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
        JButton btn = new JButton();
            board[i][j] = btn;

this is what I have attempted to code, so that a random button will be pressed

int r = (int) (Math.random() * 4);

int k = r;
int p = r;

board[k][p].doClick();

if more information is needed, you can check out the whole method I have

Color prim = new Color (255,160,122);//creating colors
//nested loop that creates the 9 buttons
for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
        JButton btn = new JButton();

        //placing i and j inside the 2d array buttons
        board[i][j] = btn;
        board[i][j].setBorder(compound);
        //board[i][j].setOpaque(false);
        btn.setFont(new Font("Arial", Font.BOLD, 58)); //changing letter font and size
        Color c = (i+j) % 2 ==1 ? prim:Color.CYAN; //changing the color of the board
        board[i][j].setBackground(c);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(((JButton)e.getSource ()).getText().equals("") && currentPlayer.contentEquals("x") && hasWinner == false) { //check if someone has won
                    btn.setText(currentPlayer); //when someone makes a move set the button to have text with their character
                    turnCounter++;
                    hasWinner(); //call hasWinner method to check if someone won
                    changePlayer(); //call changePlayer method to change the turn


                }
                else if(currentPlayer.contentEquals("o") && hasWinner == false) { //check if someone has won

                    //JOptionPane.showMessageDialog(null,"hi");

                    int r = (int) (Math.random() * 4);

                    int k = r;
                    int p = r;

                    board[k][p].doClick();


                    btn.setText(currentPlayer); //when someone makes a move set the button to have text with their character
                    turnCounter++;
                    hasWinner(); //call hasWinner method to check if someone won
                    changePlayer(); //call changePlayer method to change the turn

                    //board[0][0]
                }
            }
        });
        pane.add(btn);
    }
}

来源:https://stackoverflow.com/questions/59797599/trouble-having-2d-array-of-buttons-click-themselves-randomly-using-the-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!