简体   繁体   English

错误:从内部类访问局部变量 imagesLabel; 需要声明为 final (Java)

[英]Error: local variable imagesLabel is accessed from within inner class; needs to be declared final (Java)

Background背景

On following screenshot, the user should be able to change the image by clicking on one of the radio buttons:在以下屏幕截图中,用户应该能够通过单击单选按钮之一来更改图像:

截屏

When a radio button is clicked the picture should change to a different one .单击单选按钮时,图片应更改为不同的图片。 I have put all images into an array.我已将所有图像放入一个数组中。

Problem问题

When compiling code I have tried so far (see source below) the compiler gives following error:在编译我到目前为止尝试过的代码时(请参阅下面的源代码),编译器给出以下错误:

Error: local variable imagesLabel is accessed from within inner class;错误:从内部类访问局部变量imagesLabel needs to be declared final需要宣布为最终

And once I add final before respective variable, I got following error when compiling:一旦我在各个变量之前添加final ,编译时就会出现以下错误:

Error: <identifier> expected错误:<标识符> 预期

Java Code (after adding the final ) Java 代码(添加final

Icon[] images = new Icon[3];

// Get the images and store them in the images array.  These images 
// were manually resized to be similar in size prior to writing the
// program.
images[0] = new ImageIcon("/Users/grimygod/Desktop/negativeFilterExample.png");
images[1] = new ImageIcon("/Users/grimygod/Desktop/reflect.png");
images[2] = new ImageIcon("/Users/grimygod/Desktop/colorSubtraction.png");

// The images will be displayed as the image on a JLabel without text
JLabel imagesLabel = new JLabel();

// Set the initial image to be the negative filter, and add it to the panel,
// since the radio button for the negative filter is initially selected.
imagesLabel.setIcon(images[0]);
iconPanel.add(imagesLabel);
radioButtonPanel.add(iconPanel);

// Creation of "Select An Image & Apply Filter!" button
JButton selectButton = new JButton("Select An Image & Apply Filter!");
submitButtonPanel.add(selectButton);
radioButtonPanel.add(submitButtonPanel);

// Makes the whole window visible
testFrame.setVisible(true); 

// First button is selected when program is ran
button1.setSelected(true);

button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        final imagesLabel.setIcon(images[2]);   //  <<<<< ERROR in this line 
   }
});

You can do any of the following to resolve the issue:您可以执行以下任一操作来解决此问题:

A. Declare imagesLabel as final A. 将imagesLabel声明为final

final JLabel imagesLabel = new JLabel();

B. Declare imagesLabel as an instance variable ie outside the method. B. 将imagesLabel声明为实例变量,即在方法之外。 In other words, declare it in the class itself.换句话说,在类本身中声明它。

Make sure to remove final from final imagesLabel.setIcon(images[2]);确保从final imagesLabel.setIcon(images[2]);删除final ie it should be simply imagesLabel.setIcon(images[2]);即它应该只是imagesLabel.setIcon(images[2]);

Issue A问题 A

You are defining an anonymous inner class of type ActionListener when adding the listener, see (1) in code below.添加侦听器时,您正在定义类型为ActionListener匿名内部类,请参阅下面代码中的 (1)。 Then you try to access the variable imagesLabel which is declared outside (of the inner class), see (2) in code below.然后您尝试访问在外部(内部类)声明的变量imagesLabel ,请参阅下面代码中的(2)。 Since this variable is declared non-final , you cannot use it within an anonymous inner class like this.由于此变量声明为non-final ,因此您不能在这样的匿名内部类中使用它

► Thus compiling failed with message: ► 因此编译失败并显示消息:

Error: local variable imagesLabel is accessed from within inner class;错误:从内部类访问局部变量 imagesLabel; needs to be declared final需要宣布为最终

Code in state A (before final was added):状态 A 中的代码(在添加final之前):

button1.addActionListener(new ActionListener() { // (1)
    @Override
    public void actionPerformed(ActionEvent e) {
        imagesLabel.setIcon(images[1]);   //  (2) <<<<< ERROR in this line 
   }
});

Issue B问题 B

Trying to solve it you followed the instruction of the message and would like to "declare" the variable imagesLabel as final directly in this line - marked (3) in code below.尝试解决它,您按照消息的说明进行操作,并希望直接在此行中“声明”变量imagesLabelfinal - 在下面的代码中标记为 (3)。

But the line (3) is not the location of declaration of variable imagesLabel , rather than a method call on this instance variable.但是第 (3) 行不是变量imagesLabel的声明位置,而是对该实例变量的方法调用

The compiler reads the source code line beginning with final and expects an identifier hereafter, but there was a method call .编译器读取以final开头的源代码行,并期望此后有一个标识符,但有一个方法调用

► Thus compiling failed with message: ► 因此编译失败并显示消息:

Error: <identifier> expected错误:<标识符> 预期

Code in state B (after final was added):状态 B 中的代码(在添加final之后):

button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        final imagesLabel.setIcon(images[1]);   //  (3) <<<<< ERROR in this line 
   }
});

⚠ NOTE: same compilation error and fix (as commented by JB Nizet ) applies to variable images which is used as argument in the same line (3) above. ⚠ 注意:相同的编译错误和修复(如JB Nizet评论)适用于在上面同一行 (3) 中用作参数的可变images

Solution解决方案

You have to use the modifier final where the variable imagesLabel (as well as images array) is declared.您必须在声明变量imagesLabel (以及images数组)的地方使用修饰符final Then you can access it inside the inner class like first in code state A:然后你可以在内部类中访问它,就像代码状态 A 中的 first 一样:

final Icon[] images = new Icon[3]; // <<< here is the other declaration

/* ... lines of code left out for clarity  ... */

// The images will be displayed as the image on a JLabel without text
final JLabel imagesLabel = new JLabel(); // <<< here is the declaration

/* ... lines of code left out for clarity  ... */

button1.addActionListener(new ActionListener() { // (1) inner class
    @Override
    public void actionPerformed(ActionEvent e) {
        imagesLabel.setIcon(images[1]);   //  (2) <<<<< now you can use the final declared variable imagesLabel
   }
});

See also也可以看看

暂无
暂无

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

相关问题 “从内部类访问变量需要声明为final”错误 - "variable is accessed from within inner class needs to be declared final" error 从内部类内部访问的Java局部变量; 需要声明为final,为什么它可以在NetBeans中工作? - Java local variable accessed from within inner class; needs to be declared final, why it works in NetBeans? 错误:从内部类中访问局部变量事件; 需要宣布最终 - Error: local variable event is accessed from within inner class; needs to be declared final 错误:从内部类访问局部变量 a; 需要宣布为最终 - Error: local variable a is accessed from within inner class; needs to be declared final 变量(dialogView)是从内部类中访问的,需要声明为final - Variable (dialogView) is accessed from within inner class, needs to be declared final 从内部类访问变量“i”,需要声明为 final - Variable 'i' is accessed from within inner class, needs to be declared final 变量&#39;btnsave&#39;是从内部类中访问的,需要声明为final - Variable 'btnsave' is accessed from within inner class, needs to be declared final java:从内部类内部访问局部变量循环器; 需要声明为final,但是变量可能尚未初始化 - java: local variable looper is accessed from within inner class; needs to be declared final, but then variable might not have been initialized 从内部类访问变量“ ”,需要声明为 final - Variable ' ' is accessed from within inner class, needs to be declared final 从内部 class 访问的变量需要声明为 final - Variable accessed from within inner class needs to be declared final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM