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

标签 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.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)

最佳答案

这个:

JOptionPane.showMessageDialog(null, String.format("Answer: %s", add));

尝试使用add作为变量。但是,该变量在 actionPerformed 方法中不存在。它是 Gui 构造函数中的一个本地变量。这有点令人困惑,因为 add 也是一个方法,但从根本上来说,问题在于您在本地声明的 add 变量构造函数中的内容稍后不存在。

另请注意此比较:

if(event.getActionCommand()=="Add")

可能的工作取决于您提供命令的方式,但它实际上只是比较字符串引用。您应该使用:

if (event.getActionCommand().equals("Add"))

或者如果操作命令可能为空(并且您不想费心检查):

if ("Add".equals(event.getActionCommand())

另请注意:

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

表示您可能忽略了来自 Eclipse 的警告:“代码无法编译 - 您真的想尝试运行它吗?”

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

关于Java:声明一个变量但说它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863011/

相关文章:

java - 当我正确验证时,HTTP 禁止我访问指定资源 - JavaEE

java - 为什么包保护方法在同一个包中不可见?

java - 哪种方法是最好的?

ajax - 使 Controller 根据请求类型返回HTML或JSON

java - JFrame 上的 MouseListener 只监听边框

java - 在任意位置放置形状

java - Photoshop 图片在 Android 应用程序开发中无法运行(使用 Eclipse)

android - 单击操作后如何关闭通知

java - 如何在不定义位置的情况下读取文本文件?

java - 字符串 Java 代码检查