Java:声明一个变量,但说它不存在

Java:声明一个变量,但说它不存在,java,action,jframe,listener,handler,Java,Action,Jframe,Listener,Handler,我正在尝试制作一个基本的计算器(我没有完成),但是我得到了一个错误,在这个错误中,操作侦听器说变量“add”不存在。我已经解析了所有内容,但变量似乎没有正确声明。代码如下: import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import java.awt.FlowL

我正在尝试制作一个基本的计算器(我没有完成),但是我得到了一个错误,在这个错误中,操作侦听器说变量“add”不存在。我已经解析了所有内容,但变量似乎没有正确声明。代码如下:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

public class Gui extends JFrame{

    private JButton reg;
    private JButton reg2;
    private JButton reg3;
    private JButton reg4;
    private JTextField text;
    private JTextField text2;
    private JTextField text3;
    private JTextField text4;
    private JTextField text5;
    private JTextField text6;
    private JTextField text7;
    private JTextField text8;


    public Gui(){
        super("Buttons");
        setLayout(new FlowLayout());



        text = new JTextField("000000000000000000000");
        text2 = new JTextField("000000000000000000000");
        text3 = new JTextField("000000000000000000000");
        text4 = new JTextField("000000000000000000000");
        text5 = new JTextField("000000000000000000000");
        text6 = new JTextField("000000000000000000000");
        text7 = new JTextField("000000000000000000000");
        text8 = new JTextField("000000000000000000000");
        reg = new JButton("Add");
        reg2 = new JButton("Divide");
        reg3 = new JButton("Multiply");
        reg4 = new JButton("Subtract");

        add(text);
        add(text2);
        add(reg);
        add(text3);
        add(text4);
        add(reg2);
        add(text5);
        add(text6);
        add(reg3);
        add(text7);
        add(text8);
        add(reg4);

        String st = text.getText();
        String st2 = text.getText();
        String st3 = text.getText();
        String st4 = text.getText();
        String st5 = text.getText();
        String st6 = text.getText();
        String st7 = text.getText();
        String st8 = text.getText();

        int txt1 = Integer.parseInt(st);
        int txt2 = Integer.parseInt(st2);
        int txt3 = Integer.parseInt(st3);
        int txt4 = Integer.parseInt(st4);
        int txt5 = Integer.parseInt(st5);
        int txt6 = Integer.parseInt(st6);
        int txt7 = Integer.parseInt(st7);
        int txt8 = Integer.parseInt(st8);

        int add=txt1+txt2;
        if(txt4>0){
            int add2=txt3/txt4;
        }
        int add3=txt5*txt6;
        int add4=txt7-txt8;


        handlerClass handler = new handlerClass();
        reg.addActionListener(handler);
        reg2.addActionListener(handler);
        reg3.addActionListener(handler);
        reg4.addActionListener(handler);






    }

    public class handlerClass implements ActionListener{
        public void actionPerformed(ActionEvent event){
            if(event.getActionCommand()=="Add"){
                JOptionPane.showMessageDialog(null, String.format("Answer: %s", add));
            }
        }
    }
}
这是错误代码

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    add cannot be resolved to a variable

    at Gui$handlerClass.actionPerformed(Gui.java:101)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    add cannot be resolved to a variable

    at Gui$handlerClass.actionPerformed(Gui.java:101)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    add cannot be resolved to a variable

    at Gui$handlerClass.actionPerformed(Gui.java:101)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

它还应该说
add2
不存在


ActionListener
实现中,您无权访问构造函数中声明的本地
add
变量。它应该是一个实例变量、一个静态变量等等。

Aaron,相信编译器。你的代码错了,错了,错了

public class handlerClass implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(event.getActionCommand()=="Add"){
            JOptionPane.showMessageDialog(null, String.format("Answer: %s", add));
        }
    }
}
actionPerformed的作用域中没有名为add的变量。您在构造函数中声明的是一个局部变量,在此处不可见。

此:

JOptionPane.showMessageDialog(null, String.format("Answer: %s", add));
尝试使用
add
作为变量。但是,
actionPerformed
方法中不存在该变量。它是
Gui
构造函数中的局部变量。这有点让人困惑,因为
add
也是一种方法,但从根本上说,问题是您在构造函数中本地声明的
add
变量以后不存在

还要注意,这种比较:

if(event.getActionCommand()=="Add")
可能会根据您提供命令的方式工作,但实际上只是比较
String
引用。你应使用:

if (event.getActionCommand().equals("Add"))
或者,如果action命令可能为null(并且您不想麻烦检查):

还请注意:

线程“AWT-EventQueue-0”java.lang中出现异常。错误:未解决的编译问题:无法将add解析为变量

表示您可能忽略了Eclipse发出的警告,该警告说“代码未编译-是否仍要尝试运行它?”


您不应该尝试运行未编译的代码-您应该在尝试运行它之前修复编译时错误。

如果您询问的是
add
,那么这是因为您在构造函数中声明了它

public Gui(){
...
    int add=txt1+txt2;
...
}
所以它是本地引用,不能在其他方法或内部类中使用。如果您想在那里使用它,请将其设为类字段

public class Gui extends JFrame{
...
    int add;

    public Gui(){
    ...
        int add=txt1+txt2;
    ...
    }
    public class handlerClass implements ActionListener{
        //now add will be accesible here
    }

}

实际上,在ActionListener中,
add
没有定义。它是Gui构造函数的局部变量

只有在执行操作时(即在
actionPerformed()
方法中),才应从文本字段中读取并执行添加。当前,您的代码在初始化Gui时添加文本字段中的内容,并且在执行操作时尝试显示此初始添加的结果


此外,与其在actionPerformed方法中比较按钮文本,不如为每个按钮添加一个特定的ActionListener。添加一个监听器,该监听器添加到Add按钮;一个监听器,它将减法到减法按钮,等等。

您的变量“add”在Gui的类构造函数中声明,该类构造函数从不实例化。这是程序结构和变量作用域的基本问题。

您已经在构造函数中声明了变量
add
。这意味着
add
是一个局部变量,仅在构造函数内部可见

您只需将变量
add
的声明移到外部即可。换言之,如果希望嵌套类可以看到实例变量,请将其设置为add

现在,您的实例变量将有一个以上的变量,如下所示:

private JButton reg;
private JButton reg2;
...
private JTextField text7;
private JTextField text8;


private int add;
下面的一行,您为
add
赋值的地方将从:

    int add=txt1+txt6;


请注意,如果希望其他变量对某个嵌套类可见,则必须对其他变量执行相同的操作。

我意识到我没有任何除法,乘法或减法,但我仍在处理它确切的错误信息是什么,它发生在哪一行?错误代码长到O.0,大约6k到long@AaronLeslie:如果您没有尝试运行未编译的代码,则错误代码不会那么长。编译时出现错误。你甚至不应该在那种状态下运行它。请学习如何使用代码格式。将其用于代码和异常输出。
    int add=txt1+txt6;
    add=txt1+txt2;