0

我正在尝试制作一个基本的计算器(我没有完成),但我收到一个错误,其中动作侦听器说变量“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)
4

7 回答 7

3

这个:

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

尝试add用作变量。但是,该方法中不存在该变量actionPerformed。它是构造函数中的局部变量Guiadd这也是一种方法,这有点令人困惑,但基本上问题是您在构造函数中本地add声明的变量稍后不存在。

另请注意,此比较:

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

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

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

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

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

另请注意:

线程“AWT-EventQueue-0”java.lang.Error 中的异常:未解决的编译问题:添加无法解析到变量

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

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

于 2012-06-02T14:23:14.367 回答
3

确实,在ActionListener里面,add是没有定义的。它是 Gui 构造函数的局部变量。

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

此外,与在 actionPerformed 方法中比较按钮文本不同,为每个按钮添加特定的 ActionListener 会更简洁。添加一个添加到添加按钮的侦听器;一个监听器,它减去减法按钮等。

于 2012-06-02T14:23:58.643 回答
2

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

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

在 actionPerformed 的范围内没有名为 add 的变量。您在构造函数中声明的是一个在此处不可见的局部变量。

于 2012-06-02T14:23:03.613 回答
2

如果您要问,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
    }

}
于 2012-06-02T14:23:44.837 回答
1

您的变量“add”在 Gui 的类构造函数中声明,它从未实例化。这是程序结构和变量范围的基本问题。

于 2012-06-02T14:32:38.870 回答
1

它也应该说不add2存在。

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

于 2012-06-02T14:22:52.340 回答
0

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

您需要做的就是将变量的声明移到add外面。换句话说,如果您希望它对嵌套类可见,请添加一个实例变量。

现在您的实例变量将有一个像这样的变量:

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


private int add;

和下面的行,您分配值的位置add将从:

    int add=txt1+txt6;

    add=txt1+txt2;

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

于 2012-06-08T06:39:43.243 回答