gpt4 book ai didi

java - 拖动时移动节点 setOnMouseDragged 仅在父级上触发

转载 作者:行者123 更新时间:2023-11-30 05:45:24 25 4
gpt4 key购买 nike

我有一个想要通过拖动移动的节点,当我使用 setOnMouseClicked 时,即使单击该节点的子节点,它也会触发,但 setOnMouseDragged 仅当拖动位于父节点上时才会触发:

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

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

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

最佳答案

您可以使用如下所示的内容:

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

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

MouseEvent::MOUSE_CLICKED

This event occurs when mouse button has been clicked (pressed and released on the same node). This event provides a button-like behavior to any node. Note that even long drags can generate click event (it is delivered to the top-most node on which the mouse was both pressed and released).

要阻止事件到达父级,您还可以使用 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);
}
}

关于java - 拖动时移动节点 setOnMouseDragged 仅在父级上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54930129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com