实现内部匿名操作侦听器



我一直在尝试如上所述实现此操作侦听器,但不断收到两个错误:

-Cannot instantiate the type ActionListener
-void is an invalid type for the variable incrementAction

我一直在寻找类似的例子,但它们似乎都指向相同的实现方法。这就是我必须去的地方。

        increment.addActionListener(new ActionListener());{
        public void incrementAction(ActionEvent e){
            this.incrementCount();
            this.setTextField();
        }
    }

ActionListener 方法的签名为:

public void actionPerformed(ActionEvent e)

JButton increment  = new JButton();
increment.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent received! ");
    }
});

最新更新