Inner classes in Java

I make keyboard buttons for my Hangman game ( SEE PICTURE HERE ), my problem is with inner classes. I read this LINK about inner classes and says that you can only access external variables with a type FINAL. But if I declare a variable as such, I can’t change it anymore ... So my problem is that I need to change the value inside the inner class. My code is as follows:

public class MainGame extends JDialog {

    private String player;
    private char [] wordChar;
    private JButton[] buttons;
    private int level;
    private int score;
    private int livesLeft;
    private int missedGuess;

 void newGame() {

        level = 0;
        score = 0;
        livesLeft = 10;
        missedGuess = 0;

       //label1:
       // while (livesLeft!= 0) {

            //get random WORD from LIST
            Word hiddenWord = new Word();

            //put random word in Array
            wordChar = new char[hiddenWord.getHiddenWord().length()];
            wordChar = hiddenWord.getHiddenWord().toCharArray();

            buttons = new JButton[wordChar.length];
            for (int i = 0; i < wordChar.length; i++){
                JButton guessWord = new JButton(" ");
                guessWord.setFont(new Font("Microsoft Sans Serif", 1, 18));
                guessWord.setEnabled(false);

                jPanel3.setLayout(new GridLayout(1, wordChar.length));
                jPanel3.add(guessWord);

                buttons[i] = guessWord;
            }
      checkLetter();
      }

    void checkLetter() {
         int checker = 0;
         while(checker != wordChar.length){
            jPanel1.setLayout(new GridLayout(3, 9, 3, 5));
            for (char buttonChar = 'a'; buttonChar <= 'z'; buttonChar++) {
                String buttonText = String.valueOf(buttonChar);
                final JButton letterButton = new JButton(buttonText);
                letterButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String actionCommand = e.getActionCommand();
                        for (int j = 0; j < wordChar.length; j++){
                            String text =  String.valueOf(wordChar[j]);
                            if(actionCommand.equals(text)){
                                buttons[j].setText(text);
                                checker++; //THIS CODE IS NOT POSSIBLE!!!!
                            }
                        }
                    }
                });
                jPanel1.add(letterButton);
             }
               checker++;
          }
        }

NOTE. The code above is not complete. Int checker is used to calculate how many correct letters have already been guessed, so if it is equal to the length of the word, now I can go to the next levelenter image description here

How can I re-execute the code?

+5
3

checker , increaseChecker().

: - :

1) :

public class OuterClassName {
    private int checker;

    protected void increaseChecker() {
        checker++;
    }

    void checkLetter() {
        // ...
    }

}

2) increaseChecker() checker++

+3

. actionPerformed() checkLetter(). , (, ,) , . . , .

checker . , checkLetter(), checkLetter(). , , checker actionPerformed()? , ?

+3

, . .

            letterButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    letterButtonActionPerformed(e); // create this method
                }
            });


            //.....


            // new method
            private void letterButtonActionPerformed(ActionEvent e) {
                    String actionCommand = e.getActionCommand();
                    for (int j = 0; j < wordChar.length; j++){
                        String text =  String.valueOf(wordChar[j]);
                        if(actionCommand.equals(text)){
                            buttons[j].setText(text);
                            checker++; //THIS CODE IS NOT POSSIBLE!!!!
                        }
                    }
            }
+1

All Articles