实现一个内部匿名动作监听器 [英] Implementing an inner anonymous action listener

查看:45
本文介绍了实现一个内部匿名动作监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试实现上面提到的这个动作监听器,但一直收到两个错误:

I've been trying to implement this action listener as stated above but keep receiving two errors:

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

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

I've been looking up similar examples but they all seem to point to the same method of implementing it. This is where I've got to.

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

这篇关于实现一个内部匿名动作监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆