Swing组件侦听自身与内部类 [英] Swing component listening to itself vs inner classes

查看:73
本文介绍了Swing组件侦听自身与内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在一个uni项目中得到了不好的反馈,需要一些公正的澄清;

I just got some bad feedback on a uni project and need some impartial clarification;

有人可以解释我何时应该使用(匿名)内部侦听器类和可以侦听自己的组件吗? (a与b)

Can anyone explain when I should use (anonymous) inner listener classes vs components that listen to themselves? (a vs b)

a)

public class myButton extends JButton {

    public myButton() {

        addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // handling code...

            }
        });
    }
}

b)

public class myButton extends JButton implements ActionListener {

    public myButton() {

        addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {

        // handle the event
    }
}

谢谢大家, 米奇

推荐答案

c)

JButton myButton = new JButton(); 
myButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        // handling code...
    }
}); 

这篇关于Swing组件侦听自身与内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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