拖动setOnMouseDragged上的移动节点仅在父节点上触发

时间:2019-02-28 16:23:52

标签: java javafx

我有一个要拖动的节点,当我使用setOnMouseClicked时,即使单击该节点的子节点,它也会触发,但是只有在父节点上拖动时,setOnMouseDragged才会触发:

    node.setOnMouseClicked(event -> {
        System.out.println("offset");

    });
    node.setOnMouseDragged(event -> {
        System.out.println("move");
    });

即使在节点的子节点上完成拖动事件,我也要触发它。为什么mouseClicked和mouseDragged具有不同的行为?

1 个答案:

答案 0 :(得分:1)

您可以使用类似以下的内容:

parent.setOnMouseClicked(e -> {
    if(e.isStillSincePress()){
        //executed when not from drag
    }
});

MouseEvent :: isStillSincePress检查是否按下和释放在同一节点中。

MouseEvent::MOUSE_CLICKED

  

单击鼠标按钮(在同一节点上按下并释放)时,将发生此事件。此事件为任何节点提供类似按钮的行为。请注意,即使长时间拖动也会产生click事件(它会传递到同时按下和释放鼠标的最顶层节点上)。

要阻止某个事件到达父级,您还可以使用Event::consume

----------编辑----------

添加了用于拖动的示例代码

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class DragApplication extends Application{

    private double startX = 0, startY = 0;

    @Override
    public void start( Stage primaryStage) throws Exception{
        EventHandler< MouseEvent> clicked = e -> System.out.printf( "%s clicked%n", e.getTarget());

        EventHandler< MouseEvent> pressed = e -> {
            startX = e.getX();
            startY = e.getY();
        };

        EventHandler< MouseEvent> dragged = e -> {
            Node n = (Node) e.getTarget();
            //in case of button it returns the Text node not the button
            if(n instanceof Text)
                n = n.getParent();
            n.layoutXProperty().set( n.getLayoutX() + e.getX() - startX);
            n.layoutYProperty().set( n.getLayoutY() + e.getY() - startY);
        };

        Button paneButton = new Button( "Pane Button");
        paneButton.setOnAction( a -> System.out.println( "Pane Button Action"));
        paneButton.setOnMouseClicked( clicked);
        paneButton.setOnMousePressed( pressed);
        paneButton.setOnMouseDragged( dragged);

        Pane pane = new Pane( paneButton);
        pane.setLayoutX( 200);
        pane.setOnMouseClicked( clicked);
        pane.setOnMousePressed( pressed);
        pane.setOnMouseDragged( dragged);
        pane.setStyle( "-fx-background-color: GREEN; -fx-min-width:200; -fx-min-height:200;");

        Button achoreButton = new Button( "Anchor Button");
        achoreButton.setOnAction( a -> System.out.println( "Anchor Button Action"));
        achoreButton.setOnMouseClicked( clicked);
        achoreButton.setOnMousePressed( pressed);
        achoreButton.setOnMouseDragged( dragged);

        Button transparentAchoreButton = new Button( "Transparent Anchor Button");
        transparentAchoreButton.setMouseTransparent( true);
        transparentAchoreButton.setOnAction( a -> System.out.println( "Anchor Button Action"));
        transparentAchoreButton.setOnMouseClicked( clicked);
        transparentAchoreButton.setOnMousePressed( pressed);
        transparentAchoreButton.setOnMouseDragged( dragged);

        AnchorPane anchor = new AnchorPane(achoreButton, transparentAchoreButton);
        AnchorPane.setTopAnchor( achoreButton, 10.0);
        AnchorPane.setLeftAnchor( achoreButton, 10.0);
        AnchorPane.setBottomAnchor( transparentAchoreButton, 10.0);
        AnchorPane.setRightAnchor( transparentAchoreButton, 10.0);
        anchor.setOnMouseClicked( clicked);
        anchor.setOnMousePressed( pressed);
        anchor.setOnMouseDragged( dragged);
        anchor.setStyle( "-fx-background-color: RED; -fx-min-width:200; -fx-min-height:200;");

        Pane root = new Pane(  anchor,pane);

        primaryStage.setScene( new Scene( root, 600, 400));
        primaryStage.setTitle( "Text scaling problem");
        primaryStage.show();
    }

    public static void main( String[] args){
        launch( args);
    }
}