简体   繁体   中英

Java - Is it possible to override an ActionListener in the SuperClass?

如果有两个类, Class AClass B ,B是A的子类......如果我的A类(超类)有一个带有ActionListenerJButton ,它是由匿名内部类实现的,我怎么能覆盖什么按钮在子类中?

Hmm, you could have the listener call a protected method of some sort:

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            doStuff();
        }
    });

Then you can override doStuff in the subclass. This seems simpler than mucking about with the events more than you have to.

I'm not really sure how your code looks like, but here is a rather generic "solution" (not tested):

for (ActionListener al : super.getThatButton().getActionListeners())
{
    super.getThatButton().removeActionListener(al);
}

And add a new ActionListener afterwards. I think that's what you might be looking for, but I'm not sure. I'd just add another ActionListener or make it use Actions instead of ActionListeners.

Your only option is to remove the current ActionListener and add a new one. You can't extend an anonymous inner class by definition: it's anonymous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM