单击3次后,Java从按钮中删除侦听器

单击3次后,Java从按钮中删除侦听器,java,swing,button,action,listener,Java,Swing,Button,Action,Listener,所以我想在按下按钮3次后,从按钮中删除一个侦听器。 到目前为止,我有这个 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 = n

所以我想在按下按钮3次后,从按钮中删除一个侦听器。 到目前为止,我有这个

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!");
    }
}
我在网上阅读的方式是做一个for循环,正如我在上面尝试的那样,但是我得到了一个类型不匹配。我能找到的大多数示例都是关于删除所有侦听器的,但是我只想从按钮中删除ConfusedListener。除了上面的for循环之外,我不知道怎么做。

您可以尝试:

if(clickCount > 3)
    {
        for(ActionListener listener : button.getActionListeners())
        {
            if (listener instanceOf ConfusedListener) {
                button.removeActionListener(conf);
            }
        }
    }
    else
        System.out.println("I don't know!");
您还可以在添加ConfusedListener时保存它的实例,并通过

button.removeActionListener(confusedListenerInstance);

getActionListeners()
方法返回按钮的所有侦听器。而且它们并非都是
ConfusedListener
的实例。我们知道的唯一确定的事情是它们是
ActionListener
的实例。这就是你的代码不能编译的原因

现在,为什么需要一个循环来删除给定的侦听器?您只需删除正在调用的ConfusedListener。所以你只需要

public void actionPerformed(ActionEvent event) 
{
    if(clickCount > 3)
    {
        button.removeActionListener(this);
    }
    else
        System.out.println("I don't know!");
}

只需存储侦听器本身的一个实例,并使用它删除正确的侦听器:

final ConfusedListener confusedListener = new ConfusedListener();
button.addActionListener(confusedListener);
button.removeActionListener(confusedListener);
当然,如果要从
ConfusedListener
方法本身内部删除侦听器,只需传递

button.removeActionListener(this);

谢谢你的回复,但是被接受的答案非常有效:)谢谢你的回答,但是被接受的答案非常有效:)