我知道有一些问题与这个问题有些相关,但这些人的问题与我在这里遇到的问题不同。不管怎样,下面是我的全部代码,但是只有一行代码不起作用。当我使用addActionListener方法来查找何时单击了名为"btn"的按钮时,btn.setText("testing 1 2 3")
会给出这个问题标题中所示的错误。我不知道这意味着什么以及如何修复它。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class graphicsTestClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("title here");
frame.setVisible(true);
JButton btn = new JButton("click");
frame.getContentPane().add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
btn.setText("testing 1 2 3") //error is here
}
});
}
}
请帮忙!提前谢谢。
在JButton
之前添加final
。不能访问内部类中的非最终变量和非实例变量。
final JButton btn = new JButton("click");