问题
So I want to remove a listener from a button after the button has been pressed 3 times. So far I have this
class Q5
{
JFrame frame;
JButton button;
int clickCount = 0;
public static void main (String[] args)
{
Q5 example = new Q5();
example.go();
}
public void go()
{
frame = new JFrame();
button = new JButton ("Should I do it");
button.addActionListener(new ButtonPressListener());
button.addActionListener(new AngelListener());
button.addActionListener(new DevilListener());
button.addActionListener(new ConfusedListener());
frame.getContentPane().add(BorderLayout.CENTER, button);
frame.setVisible(true);
frame.setSize(400,150);
// set frame properties here
}
class ButtonPressListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
clickCount++;
}
}
class AngelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("Don't do it, you might regret it!");
}
}
class DevilListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("Go on, do it!");
}
}
class ConfusedListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(clickCount > 3)
{
for(ConfusedListener conf : button.getActionListeners())
{
button.removeActionListener(conf);
}
}
else
System.out.println("I don't know!");
}
}
The way I read online was do a for loop, as I tried above, however I get a type mismatch. Most of the examples I could find were about removing all of the listeners, however I only want to remove the ConfusedListener from the button. Other than the for loop above, I don't have any ideas of how to do it.
回答1:
The getActionListeners()
method returns all the listeners of the button. And they're not all instances of ConfusedListener
. The only sure thing we know is that they're instances of ActionListener
. That's why your code doesn't compile.
Now, why would you need a loop to remove a given listener? You simply need to remove the ConfusedListener that is being invoked. So you just need
public void actionPerformed(ActionEvent event)
{
if(clickCount > 3)
{
button.removeActionListener(this);
}
else
System.out.println("I don't know!");
}
回答2:
You could try:
if(clickCount > 3)
{
for(ActionListener listener : button.getActionListeners())
{
if (listener instanceOf ConfusedListener) {
button.removeActionListener(conf);
}
}
}
else
System.out.println("I don't know!");
You could also save the instance of the ConfusedListener when adding it and remove it via
button.removeActionListener(confusedListenerInstance);
回答3:
Just store an instance of the listener itself and use it to remove the correct listener:
final ConfusedListener confusedListener = new ConfusedListener();
button.addActionListener(confusedListener);
button.removeActionListener(confusedListener);
Of if you are removing the listener from inside a method of ConfusedListener
itself just pass this
:
button.removeActionListener(this);
来源:https://stackoverflow.com/questions/20122762/java-remove-listener-from-button-after-3-clicks