简体   繁体   English

访问java中内部类中的变量

[英]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. 我正在尝试创建一个JLabel数组,所有这些都应该在单击时隐藏。 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
          }
       });
       cpane.add(label[i]);
    }

I thought that I could overcome this by the use of this or maybe super instead of the call of label[i] within the inner method but I haven't been able to figure it out. 我认为我可以通过使用this或者super而不是内部方法中的label[i]的调用来克服这一点,但我无法弄明白。

The compilation error is: local variable i is accessed from within inner class; 编译错误是:从内部类中访问局部变量i; needs to be declared final` 需要被宣布为最终的

I'm sure that the answer must be something really silly I haven't thought of or maybe I'm making some small mistake. 我敢肯定答案肯定是我没想过的傻事,或者我犯了一些小错误。

Any help would be appreciated 任何帮助,将不胜感激

Your local variable must be final to be accessed from the inner (and anonymous) class. 您的本地变量必须是final才能从内部(和匿名)类访问。

You can change your code for something like this : 您可以更改以下内容的代码:

for (int i = 1; i < label.length; i++) {
    final JLabel currentLabel =new JLabel("label " + i); 
    currentLabel.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            currentLabel.setVisible(false);   // No more compilation error here
        }
    });
    label[i] = currentLabel;
}

From the JLS : 来自JLS:

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final . 使用但未在内部类中声明的任何局部变量,形式参数或异常参数必须声明为final

Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class. 使用但未在内部类中声明的任何局部变量必须在内部类的主体之前明确赋值(第16节)


Resources : 资源:

If you're having a problem accessing i , make another variable outside the scope of your inner-class (eg before label[i].addMouseListener(...) ): 如果您在访问i遇到问题,请在内部类的范围之外创建另一个变量(例如在label[i].addMouseListener(...) ):

for(int i=1; i<label.length; i++) {
   label[i] = new JLabel("label " + i);

   final int localI = i;
   label[i].addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
         label[localI].setVisible(false);
      }
   });
   cpane.add(label[i]);
}

you can also use getSource in your program. 你也可以在你的程序中使用getSource After this you can access your component with the help of typecasting . 在此之后,您可以在typecasting的帮助下访问您的组件。 it will reduce extra lines of code, your code will look like this 它将减少额外的代码行,您的代码将如下所示

for (int i = 1; i < label.length; i++) { 
   currentLabel.addMouseListener(new MouseAdapter(e) {
      public void mouseClicked(MouseEvent me) {
         JLabel label = (JLabel) me.getSource();
      }
   });
}

This happens because label is not specified as final . 发生这种情况是因为label未指定为final

Declare the array of labels as: 将标签数组声明为:

final JLabel[] label;

instead of: 代替:

JLabel[] label;

Your MouseAdapter is not an inner class; 你的MouseAdapter不是内部类; it's an anonymous class. 这是一个匿名课程。 Anonymous classes can only refer to final variables of their enclosing code. 匿名类只能引用其封闭代码的final变量。

匿名内部类只能访问final封闭方法的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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