java 如何禁用鼠标单击按钮动作事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15812915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:56:44  来源:igfitidea点击:

How can I disable the mouse click on the button action event?

javaswingmouseeventjbutton

提问by saplingPro

Is there a way I can disable mouse click ? In the panel there are different components and for some of the Button Click events, I want to disable the mouse click. I mean the click of the mouse doesn't have any effect on the components. I can disable using the setEnabled()function but I don't want to do that way.

有没有办法可以禁用鼠标单击?在面板中有不同的组件,对于某些按钮单击事件,我想禁用鼠标单击。我的意思是单击鼠标对组件没有任何影响。我可以禁用使用该setEnabled()功能,但我不想这样做。

Is there any way I can disable the mouse click ?

有什么办法可以禁用鼠标单击吗?

Situation :

情况 :

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {  
       //..disable the mouse click on each component present inside the panel
}

回答by Blakhar

You can add an extended ActionListener to all the buttons like this:

您可以向所有按钮添加扩展的 ActionListener,如下所示:

public abstract class ExtendedActionListener implements ActionListener{

    private static boolean disabled = false;

    public static void setDisabled(boolean disabled){
        ExtendedActionListener.disabled  = disabled;
    }

    @Override
    public final void actionPerformed(ActionEvent e){

        if(disabled)
            return;

        doSomething;

    }
}

And now just disable all the ActionListeners by calling the method setDisabled(false). The Button visual behavior doesn't change at all, but nothing happens, when you click on it.

现在只需通过调用方法禁用所有 ActionListeners setDisabled(false)。Button 视觉行为根本没有改变,但当您单击它时,什么也没有发生。

If the visual click behaviour doesn't matter, then you can just remove the MouseListeners.

如果视觉点击行为无关紧要,那么您可以删除 MouseListeners。

回答by Genzer

You can create a button group like this:

您可以像这样创建一个按钮组:

public class SingleSelectionButtonGroup {

    private final List<JButton> buttons;

    public static SingleSelectionButtonGroup group(List<JButton> buttons) {
        return new SingleSelectionButtonGroup(buttons);
    }

    public static SingleSelectionButtonGroup group(JButton...buttons) {
        return new SingleSelectionButtonGroup(Arrays.asList(buttons));
    }

    private SingleSelectionButtonGroup(List<JButton> buttons) {
        this.buttons = new ArrayList<JButton>(buttons);
        setupListener();
    }

    private void setupListener() {
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SingleSelectionButtonGroup.this.disableAllExcept((JButton) e.getSource());
            }
        };

        for (JButton button : buttons) {
            button.addActionListener(listener);
        }
    }

    private void disableAllExcept(JButton clickedButton) {
        for (JButton button : buttons) {
            if (!clickedButton.equals(button)) {
                button.setEnabled(false);
            }
        }
    }

}

And then uses it with a collection of buttons that you want to group:

然后将它与要分组的按钮集合一起使用:

public class Application {

    public void run() {
        final JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(400, 300));

        final JPanel pane = new JPanel();

        List<JButton> buttons = new ArrayList<JButton>();
        String[] texts = {"A", "B", "C"};

        for (String text : texts) {
            JButton button = new JButton(text);
            buttons.add(button);
            pane.add(button);
        }

        SingleSelectionButtonGroup.group(buttons);      
        frame.getContentPane().add(pane);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Application().run();
    }

}

回答by dhaval joshi

you should use one common class of your listener and have static method for turn listener turn on and off

你应该使用你的监听器的一个公共类,并有静态方法来打开和关闭监听器

public abstract class BaseMouseListener implements ActionListener{

private static boolean active = true;
public static void setActive(boolean active){
    BaseMouseListener.active = active;
}

protected abstract void doPerformAction(ActionEvent e);

@Override
public final void actionPerformed(ActionEvent e){
    if(active){
        doPerformAction(e);
    }
}
}

your listeners would have to implementsdoPerformedAction()

你的听众必须实施doPerformedAction()

回答by AlexR

Add empty mouse listener. This will "disable" the click because it will not have any effect.

添加空鼠标侦听器。这将“禁用”点击,因为它不会产生任何效果。