2

我需要使用 mouseReleased 方法而不是 mouseClicked 所以我需要找到一种拦截双击的方法。这是代码:

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

问题是当我双击时我也有一个单击事件。谁能知道如何解决这个问题?谢谢

4

3 回答 3

1

1.使用getClickCount()Class的method方法MouseEvent

2.这将帮助您获得点击的类型.. Single、Double、Triple 等。

有关更多详细信息,请参见此处:

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

于 2012-08-06T12:59:47.613 回答
0

双击时没有单击的简单修复

我写过一次,但它有一个缺点,你的单击变得有点迟钝

示例用法:

    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);
      }
  }
}
于 2012-08-06T13:24:15.840 回答
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();
    }           
    }
于 2013-01-20T08:29:29.197 回答