我有以下代码,在第 19 行出现错误“无法为最终变量计数分配值”,但我必须将此变量分配为最终变量才能在“监听器”中使用它。错误在哪里?
import java.awt.event.*;
import javax.swing.*;
public class ButtonTester
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton();
frame.add(button);
final int count = 0;
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
count++;
System.out.println("I was clicked " + count + " times");
}
}
ActionListener listener = new ClickListener();
button.addActionListener(listener);
frame.setSize(100,60);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}