我有一个动作监听器的代码:

import java.awt.*;
import java.applet.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class m extends Applet
{
    Button b1=new Button("click here");

    public void init()
    {
       b1.addActionListener(new ActionListener()
       {
            public void ActionPerformed(ActionEvent e)
            {
                  System.out.println("Button was clicked ");
            }
       });
       add(b1);
    }
}


产生此错误:

error: <anonymous m$1> is not abstract and does not override abstrac method actionPerformed(ActionEvent) in ActionListener


有人知道这个错误的原因是什么吗?

最佳答案

您的函数ActionPerformed应该命名为actionPerformed。 Java方法名称区分大小写。

b1.addActionListener(new ActionListener()
   {
        @Override
        public void actionPerformed(ActionEvent e)
        {
              System.out.println("Button was clicked ");
        }
   });

10-06 12:55