簡體   English   中英

如何禁用鼠標單擊按鈕動作事件?

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

有沒有辦法可以禁用鼠標單擊? 在面板中有不同的組件,對於某些按鈕單擊事件,我想禁用鼠標單擊。 我的意思是單擊鼠標對組件沒有任何影響。 我可以禁用使用setEnabled()函數,但我不想這樣做。

有什么辦法可以禁用鼠標單擊嗎?

情況 :

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

您可以向所有按鈕添加擴展的 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)禁用所有 ActionListeners 。 Button 視覺行為根本沒有改變,但當您單擊它時,什么也沒有發生。

如果視覺點擊行為無關緊要,那么您可以刪除 MouseListeners。

您可以像這樣創建一個按鈕組:

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

}

你應該使用你的監聽器的一個公共類,並有靜態方法來打開和關閉監聽器

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

添加空鼠標偵聽器。 這將“禁用”點擊,因為它不會產生任何效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM