Java Actionevent最终变量?
我有下面的代码,第19行出现错误“无法将值分配给最终变量计数”,但我必须将此变量分配为最终变量,以便在“侦听器”中使用它。错在哪里Java Actionevent最终变量?,java,swing,awt,Java,Swing,Awt,我有下面的代码,第19行出现错误“无法将值分配给最终变量计数”,但我必须将此变量分配为最终变量,以便在“侦听器”中使用它。错在哪里 import java.awt.event.*; import javax.swing.*; public class ButtonTester { public static void main(String[] args) { JFrame frame = new JFrame(); JButton butto
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);
}
}
您正在
ClickListener
类中使用count,但它是在类之外声明的。您只在ClickListener
中使用它,因此将声明移到那里。还可以将类设置为静态:
static class ClickListener implements ActionListener
{
private int count = 0;
public void actionPerformed(ActionEvent event)
{
count++;
System.out.println("I was clicked " + count + " times");
}
}
您正在
ClickListener
类中使用count,但它是在类之外声明的。您只在ClickListener
中使用它,因此将声明移到那里。还可以将类设置为静态:
static class ClickListener implements ActionListener
{
private int count = 0;
public void actionPerformed(ActionEvent event)
{
count++;
System.out.println("I was clicked " + count + " times");
}
}
在ClickListener中移动变量,这是您真正想要的。
如果将变量移到类外,它必须是final,因为它将被视为常量。将变量移到ClickListener内,这才是您真正想要的。
如果将变量移到类外,则该变量必须为final,因为它将被视为常量。final变量赋值后不能修改。解决方案是使变量成为ClickListener类的成员:
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);
class ClickListener implements ActionListener
{
int count = 0;
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);
}
}
final
变量一旦赋值,就不能修改。解决方案是使变量成为ClickListener类的成员:
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);
class ClickListener implements ActionListener
{
int count = 0;
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);
}
}
您必须将它从main方法中取出,并在
ButtonTester
类中声明它。或者,如果其中有其他代码没有显示,则可能无法将其设置为静态。如果可能的话,最好将嵌套类设置为静态。如果不能,那没关系。您必须将其从主方法中拉出,并在buttonTest
类中声明它。或者,如果其中有其他代码没有显示,则可能无法将其设置为静态。如果可能的话,最好将嵌套类设置为静态。如果你不能,那很好。