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

时间:2012-06-02 14:18:49

标签: java action jframe listener handler

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

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)

7 个答案:

答案 0 :(得分:3)

此:

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

尝试将add用作变量。但是,actionPerformed方法中的变量不存在。它是Gui构造函数中的 local 变量。它有点令人困惑,因为add 也是一个方法,但从根本上说问题是你在构造函数中声明本地add变量没有以后存在。

还要注意这个比较:

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

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

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

或者如果action命令可能为null(并且您不想打扰检查):

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

另请注意:

  

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

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

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

答案 1 :(得分:3)

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

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

此外,不是比较actionPerformed方法中的按钮文本,而是为每个按钮添加特定的ActionListener会更加清晰。添加一个添加到添加按钮的侦听器;减去减法按钮等的监听器

答案 2 :(得分: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的变量。您在构造函数中声明的是一个在此处不可见的局部变量。

答案 3 :(得分: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
    }

}

答案 4 :(得分:1)

还应该说add2不存在。

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

答案 5 :(得分:1)

你的变量“add”是在Gui的类构造函数中声明的,它永远不会被实例化。这是程序结构和变量范围的基本问题。

答案 6 :(得分: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,如果你希望它们对某些嵌套类可见,你必须对其他变量也这样做。