访问java内部类中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3901597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
access to variable within inner class in java
提问by Oscar Wahltinez
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:
我正在尝试创建一个 JLabel 数组,单击时所有这些都应该不可见。当试图通过需要访问用于声明标签的循环的迭代变量的内部类来设置鼠标侦听器时,问题就出现了。代码是不言自明的:
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; needs to be declared final`
编译错误是:局部变量 i 是从内部类内部访问的;需要声明为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
任何帮助,将不胜感激
采纳答案by Colin Hebert
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
.Any local variable used but not declared in an inner class must be definitely assigned (§16)before the body of the inner class.
任何使用但未在内部类中声明的局部变量、形式参数或异常参数都必须声明
final
。任何在内部类中使用但未声明的局部变量都必须在内部类的主体之前明确分配(第 16 节)。
Resources :
资源 :
回答by Isaac
This happens because label
is not specified as final
.
发生这种情况是因为label
未指定为final
。
Declare the array of labels as:
将标签数组声明为:
final JLabel[] label;
final JLabel[] label;
instead of:
代替:
JLabel[] label;
JLabel[] label;
Your MouseAdapter
is not an inner class; it's an anonymousclass. Anonymous classes can only refer to final
variables of their enclosing code.
你MouseAdapter
的不是内部类;这是一个匿名类。匿名类只能引用final
其封闭代码的变量。
回答by meriton
Anonymous inner classes may only access variables of the enclosing method that are final
.
匿名内部类只能访问包含final
.
回答by Colin Hebert
If you're having a problem accessing i
, make another variable outside the scope of your inner-class (e.g. before label[i].addMouseListener(...)
):
如果您在访问 时遇到问题i
,请在内部类范围之外创建另一个变量(例如 before 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]);
}
回答by Devesh Jadon
you can also use getSource
in your program. After this you can access your component with the help of typecasting
. it will reduce extra lines of code,
your code will look like this
你也可以getSource
在你的程序中使用。在此之后,您可以在 的帮助下访问您的组件typecasting
。它会减少额外的代码行,你的代码看起来像这样
for (int i = 1; i < label.length; i++) {
currentLabel.addMouseListener(new MouseAdapter(e) {
public void mouseClicked(MouseEvent me) {
JLabel label = (JLabel) me.getSource();
}
});
}