我基本上用AI和所有人制作一个tic tac toe游戏,我的系统是绘制按钮,并为每个按钮分配一个布尔值,如果它被分配给它,则为真。由X拍摄,如果是空的,则为假。它绘制了正确的大小和布局,除了在我的动作监听器中,它给了我错误:
编辑:删除决赛,仍然给出错误
"无法分配最终的局部变量,因为它是在一个中定义的 封闭式。"
在将按钮文本分配给' x'后,我将布尔值更改为true。
package myClass;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
//create window
JFrame mainWindow = new JFrame("Tic Tac Toe");
//properties of mainWindow
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setSize(600, 600); // buttons will fill entirely, 200x200 buttons
mainWindow.setLayout(new GridLayout(3, 3));
//create buttons
JButton topLeft = new JButton("");
JButton topMid = new JButton("");
JButton topRight = new JButton("");
JButton midLeft = new JButton("");
JButton midMid = new JButton("");
JButton midRight = new JButton("");
JButton botLeft = new JButton("");
JButton botMid = new JButton("");
JButton botRight = new JButton("");
//checker for if the button already has a character
boolean tmid = false;
boolean tright = false;
boolean mleft = false;
boolean mmid = false;
boolean mright = false;
boolean bleft = false;
boolean bmid = false;
boolean bright = false;
//button properties
Dimension buttonSize = new Dimension(200,200);
topLeft.setSize(buttonSize);
topMid.setSize(buttonSize);
topRight.setSize(buttonSize);
midLeft.setSize(buttonSize);
midMid.setSize(buttonSize);
midRight.setSize(buttonSize);
botLeft.setSize(buttonSize);
botMid.setSize(buttonSize);
botRight.setSize(buttonSize);
//Action listener
topLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(tleft == false){
topLeft.setText("X");
tleft = true;
}
}
});
topMid.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(tmid == false){
topMid.setText("X");
tmid = true;
}
}
});
topRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(tright == false){
topRight.setText("X");
tright = true;
}
}
});
midLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(mleft == false){
midLeft.setText("X");
mleft = true;
}
}
});
midMid.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(mmid == false){
midMid.setText("X");
mmid = true;
}
}
});
midRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(mright == false){
midRight.setText("X");
mright = true;
}
}
});
botLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(bleft == false){
botLeft.setText("X");
bleft = true;
}
}
});
botMid.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(bmid == false){
botMid.setText("X");
bmid = true;
}
}
});
botRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(bright == false){
botRight.setText("X");
bright = true;
}
}
});
//draw components
mainWindow.add(topLeft);
mainWindow.add(topMid);
mainWindow.add(topRight);
mainWindow.add(midLeft);
mainWindow.add(midMid);
mainWindow.add(midRight);
mainWindow.add(botLeft);
mainWindow.add(botMid);
mainWindow.add(botRight);
mainWindow.setVisible(true); // draw it
}
}
答案 0 :(得分:0)
有两个问题。一,你无法修改最终变量。二,必须使用final变量才能在监听器中使用范围。此代码需要严格的重构。但是,这是一个需要最少干预的快速解决方案。
删除所有boolean tleft; boolean tmid; ...
变量
在main()方法定义
之外添加此枚举enum ESide {
tleft, tmid, tright,
mleft, mmid, mright,
bleft, bmid, bright,
};
在适当的位置添加此定义
// checker for if the button already has a character
final Map<ESide, Boolean> board = new HashMap<>();
for (ESide side : ESide.values()) {
board.put(side, false);
}
使用以下内容替换侦听器,并根据需要调整tleft。但是,请认真考虑制作一个返回鼠标适配器的方法。
public void mouseClicked(java.awt.event.MouseEvent evt)
{
if (board.get(ESide.tleft).booleanValue() == false) {
topLeft.setText("X");
board.put(ESide.tleft, true);
}
}
您收到的错误将被删除。