简体   繁体   English

如果拖动或单击组件,我该如何区别?

[英]How can I difference if a component has been dragged or clicked?

I have a JPanel with many JButton components inside. 我有一个内部包含许多JButton组件的JPanel Is there a way to get the panel mouse event when a button is pressed? 当按下按钮时,是否可以获取面板鼠标事件? Another point of view: How to make button transparent to panel mouse event? 另一个观点:如何使按钮对面板鼠标事件透明? I specifically need to capture mousePressed() event of panel. 我特别需要捕获面板的mousePressed()事件。

EDIT 编辑

Context: I'm dragging the panel content through a JScrollPane (actually working), to accomplish that I needed to capture the point where mouse is pressed, so both panel and buttons have MouseListener and MouseMotionListener to capture the point and do other stuff. 上下文:我正在通过JScrollPane拖动面板内容(实际上在工作),以完成捕获鼠标按下点的操作,因此面板和按钮都具有MouseListenerMouseMotionListener来捕获该点并执行其他操作。

Issue: When I press -> drag -> release the mouse button, if the mouse is still over the button it executes whatever the button does. 问题:当我按下->拖动->释放鼠标按钮时,如果鼠标仍位于按钮上方,它将执行该按钮执行的操作。 So I want the mouse listener of the panel to be 'independent' of the button, to remove the mouse listener from the buttons. 因此,我希望面板的鼠标侦听器与按钮“独立”,以便从按钮中删除鼠标侦听器。

EDIT 2 编辑2

I just realize reading my own issue... that it will make no difference removing MouseListener to the JButton. 我只是意识到读了自己的问题……将MouseListener移到JButton上不会有任何区别。 When pressing the button if the mouse it stil over it, the actionPerformed will be executed anyway...What can I do? 当鼠标悬停在按钮上时,无论如何都会执行actionPerformed ...我该怎么办?

EDIT 3 Edited question title, according to the solution. 编辑3根据解决方案编辑问题标题。

Kipping in mind that the event execution order in this case is: mousePressed->mouseDragged->actionPerformed->mouseReleased , I get it working at the moment, adding a boolean: 请记住,这种情况下的事件执行顺序是:mousePressed-> mouseDragged-> actionPerformed-> mouseReleased,我现在使它工作,并添加了一个布尔值:

@Override
public void mousePressed(MouseEvent e) {
        origin = new Point(e.getPoint());
}
//each time the user stops dragging set dragged to false
@Override
public void mouseReleased(MouseEvent arg0) {
     dragged = false;
}

@Override
public void mouseDragged(MouseEvent e) {

        dragged=true;
        if(((Component) e.getSource()).getParent().equals(myPanel)
                || e.getSource().equals(myPanel)){
          if (origin != null) {
            JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, myPanel);
            if (viewPort != null) {
                int deltaX = origin.x - e.getX();
                int deltaY = origin.y - e.getY();

                Rectangle view = viewPort.getViewRect();
                view.x += deltaX;
                view.y += deltaY;
                myPanel.scrollRectToVisible(view);
            }
        }
}
@Override
public void actionPerformed(ActionEvent e){

    //stuff do detect the button...
    //..in case there is more than one panel, if the component belong to myPanel and dragg is false 
    if(((Component) e.getSource()).getParent().equals(myPanel)&&  dragged==false ){
    //do stuff
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM