我想为不同的玩家定制颜色;“X”和“O”。我初始化电路板的代码在这里,我不明白为什么它不起作用。运行程序时,X 和 O 都是蓝色的,即使我添加了 if/else if 循环也是如此。
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));
if(currentPlayer.equals("X")) {
button.setForeground(Color.BLUE);
}
else if (currentPlayer.equals("O")) {
button.setForeground(Color.RED);
}
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);
hasWon();
choosePlayer();
}
}
});
pane.add(button);
}
}
}
如果语言不打扰您,您可以在我的 GitHub 上查看整个代码(我在这里将一些内容翻译成英语,但在我的代码中我使用了挪威语):https : //github.com/ibrarnaseer/Tic-Tac-Toe /树/主人
弑天下
相关分类