实现内部匿名动作侦听器

时间:2014-03-02 15:52:50

标签: java swing actionlistener inner-classes anonymous-class

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

-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();
        }
    }

1 个答案:

答案 0 :(得分:1)

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! ");
    }
});