gpt4 book ai didi

java - 在方法参数java中调用二维数组

转载 作者:行者123 更新时间:2023-12-01 18:36:00 25 4
gpt4 key购买 nike

是否可以在方法中调用二维数组?

JButton[][] buttons = new JButton[3][3];
int number = 0;

for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons.length; j++){
buttons[i][j] = new JButton(); // create buttons
frame.add(buttons[i][j]);
buttons[i][j].setText(".");
buttons[i][j].setName("button" + number);
number++;
}
}

我已经尝试过,但它不起作用。

private void test(buttons[i][j]) {

}

编辑:另外,我是否需要使用相同的方式来调用其他地方的所有按钮,例如我有这个。

JButton[] button = {//call the array here of buttons here //};
Random rand = new Random();
JButton newbutton = button[rand.nextInt(button.length)];

我想调用按钮变量参数中的所有按钮。上面的代码将根据给定按钮的数量随机选择一个按钮。例如,如果我有;

JButton[] button = {button1, button2, button3, button4, button5, button6};

给出上面的代码,它将从列表中的按钮之一中进行选择,然后将所选按钮放入变量 newbutton 中。但由于我使用了数组,所以我不能在随机选择中包含button1、button2、button等。所以我想知道是否可以以某种方式调用该按钮数组内的数组。我已经尝试过了,但没有成功。

JButton[] button = {buttons[i][j]};

最佳答案

正确的签名是:

 public void doSomething( JButton[][] myArray )
{
//doSomething
}

传递具有该签名的任何变量都将在您的代码中起作用。

要获取 JButton 数组之一,您需要

 JButton[][] doubleArray = new JButton[1][1];
JButton[] singleArray = doubleArray[0];

关于java - 在方法参数java中调用二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com