Java Swing:如何从组件中删除匿名ActionListener [英] Java Swing: How to remove an anonymous ActionListener from a component

查看:134
本文介绍了Java Swing:如何从组件中删除匿名ActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用匿名 ActionListeners 创建了一个 JButtons 数组,并且在某些情况下我要删除所有 ActionListeners ,但 .removeActionListeners 方法需要 ActionListener 作为参数。我将如何删除动作侦听器?

I created an array of JButtons with anonymous ActionListeners and under certain conditions I want to remove all the ActionListeners, but the .removeActionListeners method requires an ActionListener as an argument. How would I go about removing the action listeners?

for (int i=0; i < button.length; i++){
    button[i] = new JButton();
    button[i].addActionListener(listener.new ButtonListener());
}


推荐答案

您可以通过以下方式获取: getActionListeners 方法:

You can get them with: getActionListeners method:

for( JButton currentButton: button ) {
    for( ActionListener al : currentButton.getActionListeners() ) {
        currentButton.removeActionListener( al );
    }
}

我不确定它是否会抛出 ConcurrentModificationException 但是。

I'm not sure if it will thrown a ConcurrentModificationException though.

这篇关于Java Swing:如何从组件中删除匿名ActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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