gpt4 book ai didi

java - 需要帮助在 Java 中初始化 4 维数组

转载 作者:行者123 更新时间:2023-11-30 07:34:14 26 4
gpt4 key购买 nike

我有这个

        buttons[0].setBounds(12, 161, 96, 25);  // Numerical 0
buttons[1].setBounds(12, 50, 42, 25); // Numerical 1
buttons[2].setBounds(66, 50, 42, 25); // Numerical 2
buttons[3].setBounds(120, 50, 42, 25); // Numerical 3
buttons[4].setBounds(12, 87, 42, 25); // Numerical 4
buttons[5].setBounds(66, 87, 42, 25); // Numerical 5
buttons[6].setBounds(120, 87, 42, 25); // Numerical 6
buttons[7].setBounds(12, 124, 42, 25); // Numerical 7
buttons[8].setBounds(66, 124, 42, 25); // Numerical 8
buttons[9].setBounds(120, 124, 42, 25); // Numerical 9
buttons[10].setBounds(174, 124, 50, 62);// Equals button (=)
buttons[11].setBounds(174, 50, 110, 25);// DEL Button (DEL)
buttons[12].setBounds(120, 161, 42, 25);// DOT Button (.)
buttons[13].setBounds(174, 87, 50, 25); // MINUS Button(-)
buttons[14].setBounds(234, 87, 50, 25); // PLUS Button (+)
buttons[15].setBounds(236, 124, 50, 25);// PUTA Button (*)
buttons[16].setBounds(236, 161, 50, 25);// DJEL Button (/)

我想优化它。这就是我如何使用数组和 for 循环来管理添加项目的方式。

JButton [] buttons = new JButton [17];
String [] btnCommands = {"0","1","2","3","4","5","6","7","8","9","=","DEL",".","-","+","*","/"};

for (int i = 0;i < btnCommands.length;i++) {
buttons[i] = new JButton(btnCommands[i]);
}

我如何为 setBounds 创建一个 4 维数组?

int [][][][] bounds = {{{{}}}};

如何初始化和使用?

最佳答案

我会使用JButton.setBounds(Rectangle)方法来代替,并有一个矩形数组。

String[] btnCommands = {"0","1","2","3","4","5","6","7","8","9","=","DEL",".","-","+","*","/"};
Rectangle[] bounds = {
new Rectangle(12, 161, 96, 25), // Numerical 0
new Rectangle(12, 50, 42, 25), // Numerical 1
new Rectangle(66, 50, 42, 25), // Numerical 2
new Rectangle(120, 50, 42, 25), // Numerical 3
new Rectangle(12, 87, 42, 25), // Numerical 4
new Rectangle(66, 87, 42, 25), // Numerical 5
new Rectangle(120, 87, 42, 25), // Numerical 6
new Rectangle(12, 124, 42, 25), // Numerical 7
new Rectangle(66, 124, 42, 25), // Numerical 8
new Rectangle(120, 124, 42, 25), // Numerical 9
new Rectangle(174, 124, 50, 62),// Equals button (=)
new Rectangle(174, 50, 110, 25),// DEL Button (DEL)
new Rectangle(120, 161, 42, 25),// DOT Button (.)
new Rectangle(174, 87, 50, 25), // MINUS Button(-)
new Rectangle(234, 87, 50, 25), // PLUS Button (+)
new Rectangle(236, 124, 50, 25),// PUTA Button (*)
new Rectangle(236, 161, 50, 25),// DJEL Button (/)
};

JButton[] buttons = new JButton[17];
for (int i = 0;i < btnCommands.length;i++) {
JButton b = new JButton(btnCommands[i]);
b.setBounds(bounds[i]);
buttons[i] = b;
}

关于java - 需要帮助在 Java 中初始化 4 维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637423/

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