我目前正在为战舰游戏编码,我的UI涉及按钮的二维网格。我试图这样做,以便当单击每个按钮时,该按钮的坐标将被发送到另一个方法以执行必要的命令。但是,我无法弄清楚如何让每个按钮在特定时间响应点击次数。以下是适用于我的问题的一些代码:
JPanel userFrame = new JPanel();
userFrame.setLayout(new GridLayout(10, 10));
JButton[][] userGrid = new JButton[10][10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
userGrid[i][j] = new JButton("(" + i + ", " + j + ")");
userFrame.add(userGrid[i][j]);
}
}
contentPane.add(userFrame);
JPanel computerFrame = new JPanel();
computerFrame.setLayout(new GridLayout(10, 10));
JButton[][] computerGrid = new JButton[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
computerGrid[i][j] = new JButton("(" + i + " ," + j + ")");
computerFrame.add(computerGrid[i][j]);
}
}
contentPane.add(computerFrame);
提前感谢您的帮助!