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

mpgws1up  于 2023-01-01  发布在  Java
关注(0)|答案(4)|浏览(184)

有什么方法可以禁用鼠标点击吗?面板中有不同的组件,对于一些按钮点击事件,我想禁用鼠标点击。我的意思是鼠标点击不会对组件产生任何影响。我可以禁用setEnabled()函数,但我不想这样做。
有什么方法可以禁用鼠标单击吗?
情况:

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

hi3rlvi21#

您可以向所有按钮添加扩展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;

    }
}

现在只需通过调用setDisabled(false)方法禁用所有ActionListener,按钮的视觉行为根本不会改变,但当您单击它时,什么也不会发生。
如果视觉点击行为无关紧要,那么您可以删除MouseListener。

bwntbbo3

bwntbbo32#

您可以创建如下按钮组:

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

}

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

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

}
yh2wf1be

yh2wf1be3#

您应该使用侦听器的一个公共类,并使用静态方法打开和关闭侦听器

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

您的侦听器必须实现doPerformedAction()

vxbzzdmp

vxbzzdmp4#

添加空的鼠标监听器。这将“禁用”单击,因为它不会有任何效果。

相关问题