我基本上是用AI和所有制作一個tic tac toe遊戲,而我的系統是繪製按鈕,並且爲每個按鈕分配一個布爾值,如果它被採用,它被分配爲true由X表示,如果爲空,則爲false。這引出正確的大小和佈局,但在我的動作偵聽它給我的錯誤:最終局部變量錯誤
編輯:刪除了總決賽,仍然給錯誤
"The final local variable cannot be assigned since it is defined in an enclosing type."
當我改變布爾爲真將按鈕文本分配給'x'後。
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
}
}
是的,最終變量(例如'final boolean tleft')是_final_並且在它們被初始化後可能不會被修改。 – KevinO
你認爲'最終'的含義是什麼? –