简体   繁体   English

无法在内部类中引用变量

[英]Cannot refer to a variable inside an inner class

I'm still learning java so bear with me. 我还在学习Java,所以请多多包涵。 I am trying to write a program where you click a button named "Yes" and it increases a variable Y by 1, and same with a button "No" on variable X. I'm using eclipse and WindowBuilder Pro and it gives me an error "Cannot refer to a variable inside an inner class" 我正在尝试编写一个程序,在其中单击名为“是”的按钮,它将变量Y增加1,并与变量X上的按钮“否”相同。我使用的是eclipse和WindowBuilder Pro,它为我提供了错误“无法引用内部类内部的变量”

Here's my code: 这是我的代码:

    import javax.swing.JApplet;
    import javax.swing.JButton;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFormattedTextField;


    public class qa extends JApplet {

        /**
         * Create the applet.
         */
        public qa() {
            getContentPane().setLayout(null);

            int y=0;
            int x=0;
            String s1 = String.valueOf(y);


            JButton btnYes = new JButton("YES");
            btnYes.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent arg0) {
                    ++y;
                }
            });
            btnYes.setBounds(135, 220, 85, 42);
            getContentPane().add(btnYes);

            JButton btnNo = new JButton("NO");
            btnNo.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    ++x;
                }
            });
            btnNo.setBounds(230, 220, 85, 42);
            getContentPane().add(btnNo);

            JFormattedTextField frmtdtxtfldVarible = new JFormattedTextField();
            frmtdtxtfldVarible.setText(s1);
            frmtdtxtfldVarible.setBounds(147, 130, 157, 20);
            getContentPane().add(frmtdtxtfldVarible);


        }
    }

Thanks for any help! 谢谢你的帮助!

Declare x and y as instance variables so that you don't need to declare them as final local variables: xy声明为实例变量,以便您无需将它们声明为最终局部变量:

public class qa extends JApplet
{
    private int x = 0;
    private int y = 0;

    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能在内部类中引用非最终变量 - Cannot refer to non-final variable inside an inner class 绕过'无法引用定义的内部类中的非final变量' - Bypassing 'Cannot refer to a non-final variable inside an inner class defined' 出现错误“无法在用其他方法定义的内部类中引用非最终变量” - Getting error “Cannot refer to a non-final variable inside an inner class defined in a different method” 类型无法引用用其他方法定义的内部类中的非最终变量Asortiment - Type Cannot refer to a non-final variable Asortiment inside an inner class defined in a different method “不能引用在不同方法中定义的内部类中的非最终变量buttonflag” - “Cannot refer to a non-final variable buttonflag inside an inner class defined in a different method” 不能在不同方法中定义的内部类中引用非最终变量 - Cannot refer to a non-final variable inside an inner class defined in a different method 不能在不同方法中定义的内部类中引用非final变量i - Cannot refer to a non-final variable i inside an inner class defined in a different method 无法以不同方法定义的内部类中引用非最终变量LinearLayout - Cannot refer to a non-final variable LinearLayout inside an inner class defined in a different method 无法在使用其他方法定义的内部类中引用非最终变量pcurl1 - Cannot refer to a non-final variable pcurl1 inside an inner class defined in a different method 无法在以其他方法定义的内部类中引用非最终变量lblNewLabel - Cannot refer to a non-final variable lblNewLabel inside an inner class defined in a different method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM