简体   繁体   English

尝试将ActionListener添加到buttonArray

[英]Trying to add ActionListener to a buttonArray

I am not exactly sure what is wrong with my code but, in the process of adding the ActionListeners, I get the error : "local variables referenced from an inner class must be final or effectively final." 我不确定代码到底有什么问题,但是在添加ActionListeners的过程中,出现错误:“从内部类引用的局部变量必须是final或有效的final。” Appreciate the help :). 感谢帮助:)。

for (int i = 0; i < 30; i++) {
        button[i].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == button[i]) {
                    if (game.panel[i] instanceof Treasure || game.panel[i] instanceof Mine) {
                        game.incScore(game.referTile(i));
                        if (game.panel[i] instanceof Treasure) {
                            treasurelocated++;
                            //Output onto jta
                            if (treasurelocated == 4) {
                                GEnd();
                            }
                        } else {
                            //Output onto jta found mine
                        }
                    } else {
                        //Output onto jta found nothing
                        game.blank();
                    }
                    changeS();
                    button[i].setEnabled(false);
                }
            }
        });
    }

Just after the new ActionLister() { you start defining an anonymous inner class . new ActionLister() {您开始定义一个匿名内部类 It's a "no-name" class implementing the ActionListener interface. 这是一个实现ActionListener接口的“无名称”类。 The code which follows is enclosed in the context of this inner class and must follow special rules . 后面的代码包含在此内部类的上下文中,并且必须遵循特殊的规则

The Java language specification requires that Java语言规范要求

local variables referenced from an inner class must be final or effectively final. 从内部类引用的局部变量必须是final或有效的final。

A final variable is a variable marked with the final keyword. 最终变量是带有final关键字标记的变量。 Such a variable may be assigned only once, it's virtually a constant. 这样的变量只能分配一次,实际上是一个常数。

An effectively final variable is such a variable, where adding the final keyword does not cause the compiler to complain. 有效的final变量就是这样的变量,在其中添加final关键字不会导致编译器抱怨。 In other words, it is effectively final, but just not marked with the proper keyword. 换句话说,它实际上最终的,但没有用正确的关键字标记。

Now back to your code. 现在回到您的代码。 One or more variables in your code is a local variable from the method enclosing your code which is neither final nor effectively final . 代码中的一个或多个变量是封装代码的方法中的局部变量 ,该变量 既不是final的,也不是有效的final的

Specially, your variable i is one of them for sure. 特别是,您的变量i肯定是其中之一。 You should not refer to button[i] from the code of the listener. 您不应从侦听器的代码中引用button[i] Actually, you do not need it! 实际上,您不需要它! It is guaranteed that the listener is always called on just the button the event happened on. 可以确保始终仅在事件发生的按钮上调用侦听器。 If you need a reference to game.panel[i] , put it somehow to your button (eg as a reference). 如果您需要对game.panel[i]的引用, game.panel[i]以某种方式将其放在您的按钮上(例如,作为引用)。

For more explanation , read my answer here: https://stackoverflow.com/a/24170806/2886891 . 有关更多说明 ,请在此处阅读我的答案: https : //stackoverflow.com/a/24170806/2886891 From there you may learn more about the context of the listener's code. 从那里您可以了解有关侦听器代码上下文的更多信息。 Remember: the code of the listener is called within completely different context than where it is defined, so the i variable does not have the meaning you would expect. 请记住:侦听器的代码是在与定义的上下文完全不同的上下文中调用的,因此i变量没有您期望的含义。

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

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