2012-08-06 84 views
1

我需要使用mouseReleased方法代替mouseClicked,所以我需要找到攔截雙擊的方法。這是代碼:在mouseReleased方法中檢測雙擊

public void mouseReleased(MouseEvent e) 
{ 
    if (e.getClickCount() == 2) 
     System.out.println ("Double CLICK mouseReleased"); 
    else 
    {  
     row= clientTable.rowAtPoint(e.getPoint()); 
     col= clientTable.columnAtPoint(e.getPoint()); 

     clientTable.sel(row, col); 
    } 
} 

問題是,當我雙擊我也有一個單擊事件。任何人都可以知道何解決這個問題?由於

+1

*我需要使用mouseReleased方法而不是mouseClicked *>爲什麼? – 2012-08-06 12:51:09

+0

因爲我需要檢測表中的行選擇,如果我使用mouseClicked,我遇到了拖動的問題:如果我單擊並拖動,並在拖動過程中更改了行,我將攔截單擊拖動過程中單擊的第一個單元格。你明白這個問題嗎?謝謝! – Luca 2012-08-06 13:21:46

回答

0
public void mouseClicked(MouseEvent evt) { 
    if (evt.getButton()==MouseEvent.BUTTON1){ 
     leftClick = true; clickCount = 0; 
     if(evt.getClickCount() == 2) doubleClick=true; 
     Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); 

        timer = new Timer(timerinterval, new ActionListener() { 
        public void actionPerformed(ActionEvent evt) { 
         if(doubleClick){ 
          System.out.println("double click."); 
          sb = new StringBuffer(); 
          sb.append("Double Click"); 
          clickCount++; 
          if(clickCount == 2){ 
           rfbProto.capture(); 
           clickCount=0; 
           doubleClick = false; 
          } 

         } else { 

          sb = new StringBuffer(); 
          sb.append("Left Mouse"); 
          System.out.println("single click."); 
          rfbProto.capture(); 
         } 
        }    
       }); 
       timer.setRepeats(false); 
       timer.start(); 
       if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop(); 
    }   
    } 
+0

@Luca測試我的代碼。 :) – Anonymous 2013-01-20 08:30:01

1

使用類的方法getClickCount()方法MouseEvent

2.這將有助於你得到點擊的類型 ..單人,雙人,三人,等等。

在這裏看到更多的細節:

http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseEvent.html

+2

我在我的代碼中使用方法getClickCount(),它可以工作,但是當雙擊檢測到單擊時也會被檢測到,這對我來說是個問題。謝謝! – Luca 2012-08-06 13:01:41

0

沒有simpel修復程序沒有一個單一的點擊,當你雙擊

我寫這一次,但它有一個缺點,您的一次點擊獲取升技低迷

用法示例:

JLabel label = new JLabel("test"); 
    label.addMouseListener(new SingleClickInhibitorMouseListener(some other mouselistener)); 


public class SingleClickInhibitorMouseListener implements MouseListener 
{ 
    public static final Integer CLICK_TRESHOLD = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); 

    private Collection<MouseListener> mouseListeners = new ArrayList<MouseListener>(); 

    public SingleClickInhibitorMouseListener(MouseListener listener) 
    { 
    mouseListeners.add(listener); 
    } 

    public void addMouseListener(MouseListener listener) 
    { 
    mouseListeners.add(listener); 
    } 

    public void removeMouseListener(MouseListener listener) 
    { 
    mouseListeners.remove(listener); 
    } 

    public void removeAllListeners() 
    { 
    mouseListeners.clear(); 
    } 

    public void mouseSingleClick(MouseEvent e) 
    { 
    for (MouseListener mouseListener : mouseListeners) 
     { 
     mouseListener.mouseClicked(e); 
     } 
    } 

    public void mouseDoubleClick(MouseEvent e) 
    { 
    for (MouseListener mouseListener : mouseListeners) 
     { 
     mouseListener.mouseClicked(e); 
     } 
    } 

    public boolean hasDoubleClick = false; 

    public void mouseClicked(final MouseEvent me) 
    { 
    if (me.getClickCount() == 1) 
     { 
     new Thread(new Runnable() 
     { 
     public void run() 
     { 
      try 
      { 
      Thread.sleep(CLICK_TRESHOLD); 
      SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       { 
       if (hasDoubleClick) 
        hasDoubleClick = false; 
       else 
        mouseSingleClick(me); 
       } 
      }); 
      } 
      catch (InterruptedException e) 
      { 
      mouseSingleClick(me); 
      hasDoubleClick = false; 
      } 
     } 
     }).start(); 
     } 
    else 
     { 
     if (me.getClickCount() == 2) 
     hasDoubleClick = true; 
     mouseDoubleClick(me); 
     } 
    } 

    public void mousePressed(MouseEvent e) 
    { 
    for (MouseListener mouseListener : mouseListeners) 
     { 
     mouseListener.mousePressed(e); 
     } 
    } 

    public void mouseReleased(MouseEvent e) 
    { 
    for (MouseListener mouseListener : mouseListeners) 
     { 
     mouseListener.mouseReleased(e); 
     } 
    } 

    public void mouseEntered(MouseEvent e) 
    { 
    for (MouseListener mouseListener : mouseListeners) 
     { 
     mouseListener.mouseEntered(e); 
     } 
    } 

    public void mouseExited(MouseEvent e) 
    { 
    for (MouseListener mouseListener : mouseListeners) 
     { 
     mouseListener.mouseExited(e); 
     } 
    } 
} 
+0

感謝您發佈您的代碼,但我絕對需要能夠提供良好的雙擊檢測功能,同時也是完美的單擊。謝謝! – Luca 2012-08-06 14:39:10

+1

不存在,一個計算機無法知道第一次點擊後,鼠標上的人意圖雙擊,所以你必須模擬行爲。 – Peter 2012-08-07 09:17:41