This is my source code for a tic tak toe game I am trying to make, what I would like to happen is for a player to press a Jbutton then have the button disabled. I am aware that the command is setEnbale(false) in order for it to lock, but it is not working for me. I have 9 buttons with action listener assigned to them. The program is able to distinguish between player 1 and 2 through the action listener. But when I try to lock the cells as well the " error : can not find symbol some up. " come up. What exactlyam I doing wrong?
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();
}
}
Use,
whatPlayer.setEnabled(b)
not whatPlayer.setEnable(b)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.