检查动态创建的 JButtons 的 3/9 文本是否匹配? (用于井字游戏)
Check if 3/9 texts of dynamically created JButtons matches? (for Tic Tac Toe game)
JButton button[] = new JButton[9];
for(int i = 0;i < button.length; i++){
button[i] = new JButton();
button[i].setText(Integer.toString(i+1));
frame.add(button[i]);
button[i].addActionListener(this);
}
我有 9 个名为 "button" 的 JButton。要确定获胜,我如何检查 3 个按钮(行、列或对角线)是否匹配。提前致谢。
How can I give each dynamically created button a different name so it is easier to access?
你不知道。它们在一个数组中,一个已经具有 "name"、button
的变量,因此您可以使用数组索引从数组中访问每个单独的 JButton 对象:button[2]
for例子。请注意,我自己将数组重命名为 buttons
,这样我一眼就知道它代表多个 JButton,而不仅仅是一个。
如果这是 Tic-Tac-Toe,请参阅 my answers to related questions。
补充建议:
- 我会使用 JButton 的二维数组:
private JButton[][] buttonGrid = new JButton[ROWS][COLS];
其中 ROWS 和 COLS 是 int 常量,它们都 == 3。
- 我会在嵌套的 for 循环中创建我的 JButton,以用 JButton 填充网格。
- 当我需要知道按下了哪个按钮时,我可以使用嵌套的 for 循环遍历二维数组。
例如,
private static final int ROWS = 3;
private static final int COLS = ROWS;
private JButton[][] buttonGrid = new JButton[ROWS][COLS];
然后在网格中创建按钮,使用嵌套 for 循环:
ButtonListener buttonListener = new ButtonListener();
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
buttonGrid[row][col] = new JButton(BLANK);
buttonGrid[row][col].addActionListener(buttonListener);
// add to GridLayout using JPanel
}
}
请注意,ActionListener 的 actionPerformed ActionEvent 参数可以告诉您通过调用对象上的 getSource()
按下了哪个按钮:
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// get the pushed button:
JButton selectedButton = (JButton) e.getSource();
然后 ButtonListener 可以遍历数组以查看按下了哪个按钮,再次使用嵌套的 for 循环:
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton selectedButton = (JButton) e.getSource();
int selectedRow = -1; // initialize with non-viable value
int selectedCol = -1;
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
if (selectedButton == buttonGrid[row][col]) {
selectedRow = row;
selectedCol = col;
}
}
}
}
}
JButton button[] = new JButton[9];
for(int i = 0;i < button.length; i++){
button[i] = new JButton();
button[i].setText(Integer.toString(i+1));
frame.add(button[i]);
button[i].addActionListener(this);
}
我有 9 个名为 "button" 的 JButton。要确定获胜,我如何检查 3 个按钮(行、列或对角线)是否匹配。提前致谢。
How can I give each dynamically created button a different name so it is easier to access?
你不知道。它们在一个数组中,一个已经具有 "name"、button
的变量,因此您可以使用数组索引从数组中访问每个单独的 JButton 对象:button[2]
for例子。请注意,我自己将数组重命名为 buttons
,这样我一眼就知道它代表多个 JButton,而不仅仅是一个。
如果这是 Tic-Tac-Toe,请参阅 my answers to related questions。
补充建议:
- 我会使用 JButton 的二维数组:
private JButton[][] buttonGrid = new JButton[ROWS][COLS];
其中 ROWS 和 COLS 是 int 常量,它们都 == 3。 - 我会在嵌套的 for 循环中创建我的 JButton,以用 JButton 填充网格。
- 当我需要知道按下了哪个按钮时,我可以使用嵌套的 for 循环遍历二维数组。
例如,
private static final int ROWS = 3;
private static final int COLS = ROWS;
private JButton[][] buttonGrid = new JButton[ROWS][COLS];
然后在网格中创建按钮,使用嵌套 for 循环:
ButtonListener buttonListener = new ButtonListener();
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
buttonGrid[row][col] = new JButton(BLANK);
buttonGrid[row][col].addActionListener(buttonListener);
// add to GridLayout using JPanel
}
}
请注意,ActionListener 的 actionPerformed ActionEvent 参数可以告诉您通过调用对象上的 getSource()
按下了哪个按钮:
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// get the pushed button:
JButton selectedButton = (JButton) e.getSource();
然后 ButtonListener 可以遍历数组以查看按下了哪个按钮,再次使用嵌套的 for 循环:
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton selectedButton = (JButton) e.getSource();
int selectedRow = -1; // initialize with non-viable value
int selectedCol = -1;
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
if (selectedButton == buttonGrid[row][col]) {
selectedRow = row;
selectedCol = col;
}
}
}
}
}