简体   繁体   中英

Local Variable Must be Final Java Error

I want to update a variable every time I click on a JLabel. However, I get an error because the variable is not declared locally.

Timer picTimer = new Timer(1000, new ActionListener(){

            int oldrr=0;
            int oldrc=0 ;
            @Override
            public void actionPerformed(ActionEvent e) {
                arrayWM[oldrr][oldrc].setIcon(null);
                Random random = new Random();
                arrayWM[oldrr][oldrc].setIcon(null);
                int rr = random.nextInt(3 - 0 + 1) + 0;
                int rc = random.nextInt(3 - 0 + 1) + 0;
                oldrr = rr;
                oldrc = rc;
                arrayWM[rr][rc].setIcon(new ImageIcon("img/one.jpg"));
                int score=0;

                arrayWM[rr][rc].addMouseListener(new MouseAdapter(){

                    @Override public void mouseClicked(    MouseEvent e){
                        score++;
                        lblTimer.setText(String.valueOf(score));

                      }
                    }
                  );
            }
        });

Change like this

final int[] score = {0};

and

score[0]++;

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