1 回答

TA贡献1818条经验 获得超8个赞
看起来问题出在您设置前景色的位置。你需要把它设置在里面,ActionListener因为你知道选择那个位置的玩家是谁。像这样的东西:
private void initializeBoard() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
JButton button = new JButton();
button.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 100));
board[i][j] = button;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(((JButton)e.getSource()).getText().equals("") &&
hasWon == false) {
button.setText(currentPlayer);
if(currentPlayer.equals("X")) {
button.setForeground(Color.BLUE);
} else if (currentPlayer.equals("O")) {
button.setForeground(Color.RED);
}
hasWon();
choosePlayer();
}
}
});
pane.add(button);
}
}
}
添加回答
举报