2013-04-04 84 views
1

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

有沒有什麼辦法可以禁用鼠標點擊?

現狀:

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

你的按鈕是否有任何'ActionListener'?你想讓用戶點擊按鈕,什麼都沒有發生,或者用戶不能點擊按鈕? – Genzer 2013-04-04 14:36:56

+0

你想只消耗()mouseevents ???,但是事件來自KeyBoard(Enter和Tab被實現爲加速器) – mKorbel 2013-04-04 14:54:52

+0

不,這絕對不是你應該想要的:一個行爲良好的用戶界面必須是平等的鍵盤_和_鼠標可以很好地使用。你的_real_目標是什麼? – kleopatra 2013-04-04 15:18:25

回答

-1

添加空鼠標監聽。這將「禁用」點擊,因爲它不會有任何效果。

+4

該按鈕是否仍然會收到行動事件?我明白他想要一個'ActionListener',但不會聽用鼠標觸發的動作。 – 2013-04-04 13:46:19

+0

我不理解你。讓我們假設有6個按鈕,我按下其中一個按鈕。當我按下它時,我想禁用鼠標點擊當前存在的每個按鈕。如何添加一個空鼠標偵聽器幫助?即使我將鼠標監聽器保持爲空,我仍然可以按下其他按鈕 – saplingPro 2013-04-04 13:49:49

+0

......並且爲什麼要添加空聽者? – kleopatra 2013-04-04 15:19:29

0

您可以添加一個擴展的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。按鈕的可視化行爲完全沒有改變,但是當你點擊它時沒有任何反應。

如果可視點擊行爲並不重要,那麼您可以刪除MouseListeners。

+0

已經有一種帶有啓用屬性的ActionListener類型:它被稱爲Action :-) – kleopatra 2013-04-04 15:20:42

0

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

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

} 
0

你應該使用一個通用類的你的聽衆,並有靜態方法讓轉聽者打開和關閉

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