Java 在Eclipse中将控件添加到JFrame的过程中,如何将final关键字自动添加到变量中?

Java 在Eclipse中将控件添加到JFrame的过程中,如何将final关键字自动添加到变量中?,java,eclipse,swing,Java,Eclipse,Swing,我想用Eclipse创建一个简单的GUI应用程序。我在JFrame中添加了一个JButton。我想无论何时点击按钮,按钮的文本都会变成“你好”。我使用以下代码: JButton btnNewButton = new JButton("New button"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {

我想用Eclipse创建一个简单的GUI应用程序。我在JFrame中添加了一个JButton。我想无论何时点击按钮,按钮的文本都会变成“你好”。我使用以下代码:

JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            btnNewButton.setText("Hello");
        }
    });
但我得到以下错误:

Cannot refer to a non-final variable btnNewButton inside an inner class defined in a different method

我在互联网上搜索,发现我必须将final关键字添加到变量的第一个,如下所示:

final JButton btnNewButton = new JButton("New button");

这个问题解决了,但我的问题是如何在Eclipse中将控件添加到JFrame期间自动将final关键字添加到变量?

在Eclipse中转到以下菜单:

Window -> preferences-> WindowsBuilder-> Swing -> Code Generation
变量生成部分的局部部分,检查将变量声明为“最终”


在Eclipse中,转到以下菜单:

Window -> preferences-> WindowsBuilder-> Swing -> Code Generation
变量生成部分的局部部分,检查将变量声明为“最终”


弹出窗口已经为您提供了更改修改器的选项。如何在向jframe添加控件期间添加final关键字弹出窗口已经为您提供了更改修改器的选项。如何在向jframe添加控件期间添加final关键字