gpt4 book ai didi

java - 似乎无法锁定我的 Jbutton。错误 : Cannot find symbol

转载 作者:行者123 更新时间:2023-11-30 09:10:22 25 4
gpt4 key购买 nike

这是我正在尝试制作的井字游戏的源代码,我希望玩家按下 Jbutton 然后禁用该按钮。我知道该命令是 setEnbale(false) 以使其锁定,但它对我不起作用。我有 9 个按钮,它们分配有 Action 监听器。该程序能够通过 Action 监听器区分玩家 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();
}
}

最佳答案

使用,

whatPlayer.setEnabled(b) 不是 whatPlayer.setEnable(b)

关于java - 似乎无法锁定我的 Jbutton。错误 : Cannot find symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22574563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com