简体   繁体   English

无法从 actionlistener 内部增加变量

[英]Can't increase variable from inside actionlistener

I'm new to Java and just learning I want to increase a variable by clicking a button but I'm getting the following error我是 Java 的新手,刚刚学习我想通过单击按钮来增加变量,但出现以下错误

local variables referenced from an inner class must be final or effectively final
 
The assigned value is never used

Adding final doesn't solved the problem and I'm a bit stuck why this doesn't work?添加 final 并不能解决问题,我有点困惑为什么这不起作用? can you help?你能帮我吗?

public class JavaApplication2 {
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Click");
 
        panel.setLayout(new FlowLayout());
 
        frame.add(panel);
        panel.add(button);

        int x;
        x = 0;

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("You CLicked the Button " + x + " times");  
                x = x + 1; // *getting error*
            }
        });
 
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setLocation(50,50);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Your variable X needs to be a global, static variable because you never know when the registered ActionListener will be triggered.你的变量X需要是一个全局的 static 变量,因为你永远不知道注册的ActionListener什么时候会被触发。 X is not in that scope and might be already removed at that time. X不在那个 scope 中,并且当时可能已经被删除。

I strongly suggest using something like the following.我强烈建议使用类似下面的东西。

You shouldn't use global static variables for such a problem.对于此类问题,您不应使用全局 static 变量。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JavaApplication2 {
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Click");

        panel.setLayout(new FlowLayout());

        frame.add(panel);
        panel.add(button);

        Counter c = new Counter();

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("You CLicked the Button " + c.getValue() + " times");
                c.increment();
            }
        });

        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setLocation(50, 50);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

class Counter {
    private int value = 0;

    void increment() {
        value++;
    }

    void reset() {
        value = 0;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

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

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