私は基本的にチックタックのつま先ゲームをAIとすべてで作成しています。私のシステムではボタンを描き、各ボタンにブール値を割り当てて、 Xで、空であればFALSEを返します。それが正しいサイズとレイアウトとすべてに引き出すが、私のアクションリスナーにそれは私にエラーを与える:最終的なローカル変数のエラー
編集:私はtrueにブール値を変更すると、まだエラーに
"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
「最終的に」とはどういう意味ですか? –