简体   繁体   English

即使在同一类中声明了对象也无法找到对象

[英]Unable to find object even though it is declared in same class

I'm currently learning how to build GUI's in Java and I'm encountering the following error "cannot find symbol - Class Listener". 我目前正在学习如何在Java中构建GUI,并且遇到以下错误“找不到符号-类监听器”。

The following code creates radio buttons and then pairs them with a JLabel and an action listener. 以下代码创建单选按钮,然后将它们与JLabel和操作侦听器配对。 However even though I provide listener as an argument to button, the compiler is telling me it cannot find the listener class. 但是,即使我将监听器作为button的参数提供,编译器仍在告诉我找不到监听器类。 Does anyone know why this is? 有人知道为什么是这样吗?

The error is occuring on the parameter line of the button method. 在按钮方法的参数行上发生错误。 Any assistance is greatly appreciated. 非常感谢您的协助。

private void build(Stadium stadium)
    {  

       Listener listener = new Listener();

       add(button("Front", listener));
       add(button("Middle", listener));
       add(button("Back", listener));

    }

    private JRadioButton button(String label, Listener listener)
    {   JRadioButton button = new JRadioButton(label);
        button.addActionListener(listener);
        group.add(button);
        return button;  }

Use ActionListener not Listener , their is nothing like Listener in GUI. 使用ActionListener而不是Listener ,它们与GUI中的Listener

private void build(Stadium stadium)
{  

   ActionListener listener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            //Here you implement what you want your listener to execute on  button click;
        }
    };

   add(button("Front", listener));
   add(button("Middle", listener));
   add(button("Back", listener));

}

private JRadioButton button(String label, Listener listener)
{   JRadioButton button = new JRadioButton(label);
    button.addActionListener(listener);
    group.add(button);
    return button;  }

You need an ActionListener, not a Listener. 您需要一个ActionListener,而不是Listener。 Your class should implement ActionListener ( public class GUI implements ActionListener { ) 您的类应该实现ActionListener( public class GUI implements ActionListener {

and then implement the actionPerformed method. 然后实现actionPerformed方法。

add the instance of this class as listener. 将此类的实例添加为侦听器。

您可能需要导入Listener

暂无
暂无

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

相关问题 即使声明了类(字体)和方法(字体)也找不到符号? - Cannot find symbol even though class (Font) & method (font) are declared? 即使存在明确的活动类别也无法找到 - Unable to find explicit activity class even though it exists 测试 class 中未声明的方法,即使它在原始 class 中声明 - Undeclared method in test class even though it's declared in original class 为什么@SpringBootApplication类中声明的bean即使不是构造型类也要注册? - Why is a bean declared in @SpringBootApplication class registered even though it is not a stereotyped class? Java:即使该方法稍后在类中声明,也无法找到该方法的符号。 其余的代码正在寻找一个类。 - Java: Cannot find a method's symbol even though that method is declared later in the class. The remaining code is looking for a class. 即使它们在同一个 package 中也找不到符号 - Cannot Find Symbol even though they are in the same package Android:无法找到显式活动 class,即使我在 AndroidManifest.xml 中定义了它? - Android: Unable to find explicit activity class even though I defined it in AndroidManifest.xml? 无法从Hashmap获取对象的值,即使它返回相同的哈希码 - Unable to get value for an object from Hashmap, even though it returns the same hashcode 即使存在主类,Java 也找不到主类 - Java cannot find main class even though main class is present 为什么Hadoop即使存在也无法在本地模式下找到该文件? - Why is Hadoop unable to find this file in local mode even though it exists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM