我正在尝试使用空格键和 输出字母表。使用循环创建按钮到虚拟键盘中。但是,“w”按钮不会显示,我不知道为什么。所有其他按钮都会显示。
我的代码摘录如下所示。
String[] chars =
{"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l",".","z","x","c","v","b","n","m"," "};
while (x<28)
{
JButton button = new JButton(chars[z]);
pane.add(button, gBC);
button.addActionListener(this);
button.setActionCommand(chars[z]);
gBC.gridx = z%10;
gBC.gridy = z/10;
x ++;
z ++;
}
JButton space = new JButton();
gBC.gridx = 0;
gBC.gridy = 4;
gBC.gridwidth = 10;
pane.add(space, gBC);
提前非常感谢!
最佳答案
根据我在本地所做的测试,你应该移动。
gBC.gridx = z%10;
gBC.gridy = z/10;
到循环的开始。这给了我想要的结果。 你总是能一次性掌握定位。 您基本上可以使用前一个元素的值来计算下一个元素的位置。
while (x < 28) {
gBC.gridx = z % 10;
gBC.gridy = z / 10;
JButton button = new JButton(chars[z]);
pane.add(button, gBC);
button.addActionListener(this);
button.setActionCommand(chars[z]);
x++;
z++;
}
关于java - 我的数组中的位置 [1] 无法注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34265854/