简体   繁体   中英

Final local variable error

I'm basically making a tic tac toe game, with AI and all, and my system is to draw the buttons, and have a Boolean assigned to each button, where it is assigned to true if it's taken by an X, or false if it's empty. It draws out to the correct size and layout and all but in my action listeners it gives me the error:

Edit: removed the finals, still giving the error

"The final local variable cannot be assigned since it is defined in an enclosing type."

when I change the boolean to true after assigning the button text to '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
    }


}

There are two problems. One, you cannot modify final variables. Two, you have to use final variables to have scope within the listeners. This code needs serious refactoring. However, here is a quick solution that requires minimal intervention.

  1. Remove all of the boolean tleft; boolean tmid; ... boolean tleft; boolean tmid; ... boolean tleft; boolean tmid; ... variables

  2. Add this enum outside of the main() method definition

    enum ESide { tleft, tmid, tright, mleft, mmid, mright, bleft, bmid, bright,

    };

  3. Add this definition at the appropriate spot

    // 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); }
  4. Replace the listeners with the following, adjusting the tleft as appropriate. However, seriously consider making a method that returns the mouse adapter.

     public void mouseClicked(java.awt.event.MouseEvent evt) { if (board.get(ESide.tleft).booleanValue() == false) { topLeft.setText("X"); board.put(ESide.tleft, true); } }

The error you are receiving will be eliminated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM