java的后面3次点击来自按钮删除监听器 [英] Java remove listener from button after 3 clicks

查看:1711
本文介绍了java的后面3次点击来自按钮删除监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想从一个按钮删除侦听器按钮后就一直pressed 3倍。
到目前为止,我有这个

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!");
    }
}

我看网上的方法是循环做了,正如我上面试过,但是我得到一个类型不匹配。大部分的例子我能找到的是关于消除所有的听众,但是我只想从按钮移除ConfusedListener。除上述的for循环,我没有怎么做。任何想法

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.

推荐答案

getActionListeners()方法返回按钮的所有听众。他们不是 ConfusedListener 的所有实例。我们所知道的唯一确定的事情是,他们的ActionListener 的实例。这就是为什么你的code不能编译。

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.

现在,你为什么会需要一个循环,除去给定的监听器?你只需要删除调用此ConfusedListener。所以,你只需要

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!");
}

这篇关于java的后面3次点击来自按钮删除监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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