【发布时间】:2015-08-26 02:01:39
【问题描述】:
所以在我的 Java Swing 应用程序中,我需要一个按钮 ActionListener 才能访问其范围之外的变量,如下所示:
int x = 13;
JButton btn = new JButton("New Button");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(x);
}
});
但我得到一个变量超出范围错误。如何访问它?
【问题讨论】:
-
错误是什么?请先阅读此内容stackoverflow.com/help/how-to-ask,然后编辑您的问题。
-
你在哪里声明
int x?