作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我正在尝试制作的井字游戏的源代码,我希望玩家按下 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/
我是一名优秀的程序员,十分优秀!