简体   繁体   English

似乎无法让我的 Jbutton 锁定。 错误:找不到符号

[英]Cant seem to get my Jbutton to lock. Error: Cannot find symbol

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.这是我正在尝试制作的井字游戏的源代码,我希望发生的是让玩家按下 J 按钮然后禁用该按钮。 I am aware that the command is setEnbale(false) in order for it to lock, but it is not working for me.我知道该命令是 setEnbale(false) 以使其锁定,但它对我不起作用。 I have 9 buttons with action listener assigned to them.我有 9 个按钮,分配给它们的动作侦听器。 The program is able to distinguish between player 1 and 2 through the action listener.该程序能够通过动作侦听器区分玩家 1 和 2。 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) whatPlayer.setEnabled(b)不是whatPlayer.setEnable(b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM