Java - 找不到实现 ActionListener 的内部 class 的符号

Java - Cannot find the symbol of an inner class that implements an ActionListener

我将根据我在教科书中看到的内容为按钮制作动作侦听器。为此,我制作了一个内部 class。当我尝试调用内部 class 时,出现错误:找不到符号。

代码如下:

package GUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ATMGUI extends GUI
{

    public ATMGUI()
    {

    this.makePane();
      this.makeButton("Withdraw");
        button.addActionListener(new WithdrawListener());
        pane.add(button);
      this.makeText("Enter amount to withdraw: ");
        pane.add(text);
      this.makeTextField("Enter amount here");
        pane.add(field);
    this.makeFrame();
    frame.add(pane);

    class WithdrawListener implements ActionListener
    {
        public void actionPerformed(ActionEvent click)
        {
        System.out.println("This is a test.");
        }
    }
    }
//------------------------------------------------------------------
    public void makeFrame()
    {
    frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(500, 500);
      frame.setVisible(true);
    }

    public void makePane()
    {
    pane = new JPanel();
      pane.setLayout(new GridLayout(3,3));
      pane.setVisible(true);
    }

    public void makeButton(String buttonName)
    {
    button = new JButton(buttonName);
    }

    public void makeText(String addText)
    {
    text = new JLabel(addText);
    }

    public void makeTextField(String addText)
    {
    field = new JTextField(addText);
    }
}

这是给我带来麻烦的特别之处

button.addActionListener(new WithdrawListener());

我在别处看到它必须以某种方式实例化。 我试过类似的东西:

    ATMGUI a = new ATMGUI();
    ATMGUI.WithdrawListener w = a.new WithdrawListener();

然后将 w 放入参数中,但这对我也不起作用。 不确定是不是因为我在 subclass 工作。也不确定是否需要做不同的事情,因为我正在使用一个界面。

在本地 class 声明后向按钮添加侦听器。

public void abc(){
    PQR pqr = new PQR(); // incorrect

    class PQR {

    }   
}

正确的方法是

public void abc(){
    class PQR {

    }
    PQR pqr = new PQR(); //correct
}

WithdrawListener 放在构造函数上下文之外

public class ATMGUI extends GUI {

    public ATMGUI() {
        //...
        button.addActionListener(new WithdrawListener());  
        //...
    }

    class WithdrawListener implements ActionListener {

        public void actionPerformed(ActionEvent click) {
            System.out.println("This is a test.");
        }
    }

看来你必须在使用之前声明本地 class。我测试的以下代码片段:

这个没有错误:

public void testFunc() {
    class Test {
    }
    Test test = new Test();
}

但是这个确实如此:

public void testFunc() {
    Test test = new Test();
    class Test {
    }
}

编辑:抱歉几乎同时发帖,下次有发帖我再三刷

当您不重复使用 class 时,建议使用匿名类型。

看看它 (frequently used with listeners) - 这是一个很好的答案!! 引自上文link

Using this method makes coding a little bit quicker, as I don't need to make an extra class that implements ActionListener -- I can just instantiate an anonymous inner class without actually making a separate class.

I only use this technique for "quick and dirty" tasks where making an entire class feels unnecessary. Having multiple anonymous inner classes that do exactly the same thing should be refactored to an actual class, be it an inner class or a separate class.

this.makePane();
      this.makeButton("Withdraw");
        button.addActionListener(new ActionListener() { //starts here

            public void actionPerformed(ActionEvent click)
            {
            System.out.println("This is a test.");
            }
        });//ends
        pane.add(button);
      this.makeText("Enter amount to withdraw: ");
        pane.add(text);
      this.makeTextField("Enter amount here");
        pane.add(field);
    this.makeFrame();
    frame.add(pane);