java - 不能引用非最终变量和计时器

标签 java timer anonymous-class

我正在尝试编写一个静态类,它可以更改控制背景,该控制背景将传递给参数。所以我实现了这个:

public static void wrong(final Component component) {

        component.setBackground(Color.RED);
        Timer   timer = new Timer(2, wrongAction);

        wrongAction = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int green = component.getBackground().getGreen();
                int blue = component.getBackground().getBlue();
                component.setBackground(new Color(255, green + 1, blue + 1));
                if (component.getBackground() == Color.WHITE) {
                    timer.stop();
                }
            }
        };

        timer.start();

    }  

我遇到了一个错误:

Cannot refer to a non-final variable timer inside an inner class defined in a different method

当然,我们可以将计时器更改为最终计时器,但这样做后方法将停止工作。
我尝试用 google 搜索并在其他 stackoverflow 主题中找到答案,但没有任何帮助。

提前非常感谢大家!

最佳答案

问题是您使用了不同的错误操作引用。

public static void wrong(final Component component) {

    component.setBackground(Color.RED);
    Timer   timer = new Timer(2, wrongAction);// <-- Here wrongAction is not the one you
                                              // define on the next line

    wrongAction = new ActionListener() { // <-- This is a new ActionListener but Timer
                                         // has no knowledge about it.
        @Override
        public void actionPerformed(ActionEvent e) {

            int green = component.getBackground().getGreen();
            int blue = component.getBackground().getBlue();
            component.setBackground(new Color(255, green + 1, blue + 1));
            if (component.getBackground() == Color.WHITE) {
                timer.stop();
            }
        }
    };

    timer.start();

}

下面的代码将立即运行(但我觉得不太干净,最好将所有这些封装在一个专用对象中,以便 Timer 可以成为类的变量并且监听器可以引用它) :

public static void wrong(final Component component) {
        class MyActionListener implements ActionListener {
            private Timer timer;

            public void setTimer(Timer timer) {
                this.timer = timer;
            }

            @Override
            public void actionPerformed(ActionEvent e) {

                int green = component.getBackground().getGreen();
                int blue = component.getBackground().getBlue();
                component.setBackground(new Color(255, green + 1, blue + 1));
                if (component.getBackground().equals(Color.WHITE)) {
                    if (timer == null) {
                        System.err.println("This sucks, I got no timer");
                    } else {
                        timer.stop();
                    }
                }

            }
        }
        MyActionListener wrongAction = new MyActionListener();
        component.setBackground(Color.RED);
        Timer timer = new Timer(2, wrongAction);
        wrongAction.setTimer(timer);


        timer.start();

    }

关于java - 不能引用非最终变量和计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10587316/

相关文章:

java - 如何使用 Java Swing 实现责任链

java - Tomcat Web 应用程序管理器未列出已部署的应用程序

java - 哪个是在 lucene 中索引 boolean 值的最佳选择?

匿名对象方法的Javadoc

java - 重用 Java 的闭包/匿名类来提高性能?

java - 匿名类混淆的动态构建

Java 字符串 float

c# - 如何计算程序运行时间

c# - 文本框的计时器/刷新功能

javascript - 每小时倒数计时器,但以 30 分钟标记