access to variable within inner class in java
问题 I'm trying to create an array of JLabels, all of them should go invisible when clicked. The problem comes when trying to set up the mouse listener through an inner class that needs access to the iteration variable of the loop used to declare the labels. Code is self-explanatory: for(int i=1; i<label.length; i++) { label[i] = new JLabel("label " + i); label[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { label[i].setVisible(false); // compilation error here }