我创建了一个JButtons匿名数组,ActionListeners在某些条件下我想删除所有ActionListeners,但该.removeActionListeners方法需要ActionListener一个参数.如何删除动作侦听器?
for (int i=0; i < button.length; i++){
button[i] = new JButton();
button[i].addActionListener(listener.new ButtonListener());
}
Run Code Online (Sandbox Code Playgroud)
Osc*_*Ryz 18
您可以使用以下命令获取它们:getActionListeners方法:
for( JButton currentButton: button ) {
for( ActionListener al : currentButton.getActionListeners() ) {
currentButton.removeActionListener( al );
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定它是否会抛出一个ConcurrentModificationException.
你不能。没有人引用这些对象。为了能够删除它们,您需要将其存储为代码中的数据成员/变量,然后将该变量传递给方法removeActionListener()。但是,您可以做的是使用该getActionListeners()方法来获取ActionListener与Button. 然后,您需要弄清楚要删除哪一个,但如果只有一个,那应该很容易;-)
| 归档时间: |
|
| 查看次数: |
27857 次 |
| 最近记录: |