[英]Cant seem to get my Jbutton to lock. Error: Cannot find symbol
這是我正在嘗試制作的井字游戲的源代碼,我希望發生的是讓玩家按下 J 按鈕然后禁用該按鈕。 我知道該命令是 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();
}
}
用,
whatPlayer.setEnabled(b)
不是whatPlayer.setEnable(b)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.