这是我想要制作的tic tak脚趾游戏的源代码,我想要发生的是玩家按下Jbutton然后禁用该按钮。我知道命令是setEnbale(false)以便它锁定,但它对我不起作用。我有9个按钮,分配了动作监听器。该程序能够通过动作监听器区分玩家1和2。但是,当我试图锁定细胞时,“错误:无法找到一些符号。”出现。究竟我做错了什么?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame
{
private final int HEIGHT = 450;
private final int WIDTH = 500;
private static JButton [] button = new JButton[9];
private static Action [] playerTurn = new Action[9];
private static JLabel [] label;
private int player = 1;
private static int lockButtons = 0;
public TicTacToe ()
{
setTitle( " Tic Tac Toe ");
setSize( HEIGHT, WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4,3));
int num = 0;
for(int i = 0; i < 9; i++ )
{
button[i] = new JButton( "B" + i + 1);
playerTurn[i] = new Action();
add(button[i]);
button[i].addActionListener(playerTurn[i]);
}
setVisible(true);
}
private class Action implements ActionListener
{
public void actionPerformed(ActionEvent playerMove)
{
//Get button pressed using GetSource Command
JButton whatPlayer=(JButton)(playerMove.getSource());
if(player == 1)
{
player++;
whatPlayer.setText("player1");
whatPlayer.setEnable(false); // this is what is cause me the error
return;
}
JOptionPane.showMessageDialog(null,"Thank You For Your Input");
if (player == 2)
{
player--;
whatPlayer.setText("player2");
return;
}
}
}
public static void main(String[] arg)
{
new TicTacToe();
}
}
答案 0 :(得分:2)
使用,
whatPlayer.setEnabled(b)
不是whatPlayer.setEnable(b)