简体   繁体   English

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

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

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
}

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) .现在只需通过调用方法setDisabled(false)禁用所有 ActionListeners 。 The Button visual behavior doesn't change at all, but nothing happens, when you click on it. Button 视觉行为根本没有改变,但当您单击它时,什么也没有发生。

If the visual click behaviour doesn't matter, then you can just remove the MouseListeners.如果视觉点击行为无关紧要,那么您可以删除 MouseListeners。

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();
    }

}

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 implements doPerformedAction()你的听众必须实现doPerformedAction()

Add empty mouse listener.添加空鼠标侦听器。 This will "disable" the click because it will not have any effect.这将“禁用”点击,因为它不会产生任何效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JTable的单元格上禁用鼠标单击事件? - How to disable mouse click event on cells of JTable? 如何在 SwingGui 中调用鼠标右键单击事件? - How can I call the mouse right click event in SwingGui? java - 如何在按钮上获得鼠标按下事件 - How can I get mouse pressed event in java on a button 如何在点击事件中禁用按钮运行时? - How to disable button runtime in click event? 如何在Wicket框架中的ajaxfallbacklink上禁用右键单击事件? - How can I disable right click event on ajaxfallbacklink in Wicket framework? 如何让浮动操作按钮在每次点击时旋转 - How can I make Floating Action Button rotate on every click 如何禁用 JTable 鼠标单击? - How to disable JTable mouse click? 使用另一个按钮 - HYBRIS 进行操作时,如何禁用后台中的按钮? - How can I disable button in backoffice when make action with another button - HYBRIS? 当按下菜单按钮时,如何创建单击事件和双击事件? - How can I create a Single Click Event and Double Click Event when the Menu Button is pressed? 我可以在GXT中的鼠标单击事件中将ContentPanel添加到ContentPanel中 - Can I add a ContentPanel into a ContentPanel on a mouse click event in GXT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM