gpt4 book ai didi

JavaFx:检查鼠标是否位于节点的子节点上

转载 作者:行者123 更新时间:2023-12-02 04:01:36 24 4
gpt4 key购买 nike

JavaFx:我有一个 HBox,其中有一个按钮,在一个组内,我想为每个节点以不同的方式管理“MousePressed”事件。目前,如果我为所有节点处理相同的事件,它总是被父节点(组)捕获。

有办法做到这一点吗?例如,有没有办法确定鼠标坐标是否位于节点的子节点上方(在我的示例中为 HBox)?

这是我需要的示例:

enter image description here

如果我点击组,如果鼠标坐标不与HBox碰撞,我想隐藏HBox,如果我点击HBox,Hbox不需要隐藏,如果我点击按钮(实现作为 ImageView)我需要执行代码而不隐藏 HBox。

例如,我想做的是这样的:

    '
Group group= new Group();
HBox hBox = new HBox();
ImageView image= new ImageView();
hBox.getChildren().add(image);
hBox.toFront();
group.getChildren().add(hBox);
image.setImage(ImageUtil.getImage("img.png"));
group.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
<if not collide with HBOX hide HBOX.>

}
});
box.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
<do other things without hidding HBOX.>

}
});



image.onMouseClickedProperty().set(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
<do something>
}

});

'组位于使用 sceneBuilder 设计的 AncorPane 内的位置'

@FXML
Group group;

'

感谢您的帮助。

最佳答案

您的问题不清楚,但如果对您有帮助:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

// the magic: use setPickOnBounds(false);
public class LayersWithMouseEvents extends Application {


@Override
public void start(Stage primaryStage) {

// root
StackPane root = new StackPane();

// layers
Pane bottomLayer = new Pane();
Pane topLayer = new Pane();

// bottom: layer1, top: layer2
root.getChildren().addAll(bottomLayer, topLayer);

// layer 1 objects
Rectangle rectangle = new Rectangle( 100,100,100,100);
rectangle.setFill(Color.GREEN);
rectangle.setOnMousePressed(e -> System.out.println("Rectangle 0: " + e));
bottomLayer.getChildren().add(rectangle);

// layer 2 objects
rectangle = new Rectangle( 50,50,50,50);
rectangle.setFill(Color.RED);
rectangle.setOnMousePressed(e -> System.out.println("Rectangle 1: " + e));
topLayer.getChildren().add(rectangle);

rectangle = new Rectangle( 125,125,50,50);
rectangle.setFill(Color.RED);
rectangle.setOnMousePressed(e -> System.out.println("Rectangle 2: " + e));
topLayer.getChildren().add(rectangle);

rectangle = new Rectangle( 200,200,50,50);
rectangle.setFill(Color.RED);
rectangle.setOnMousePressed(e -> System.out.println("Rectangle 3: " + e));
topLayer.getChildren().add(rectangle);

// layer 1 event handler
bottomLayer.setOnMousePressed(e -> System.out.println("Layer 1: " + e));

// layer 2 event handler
topLayer.setOnMousePressed(e -> System.out.println("Layer 2: " + e));


// this is the magic that allows you to click on the layer1 object event though layer 2 is on top of layer 1
// but ONLY(!) as long as the layer is transparent. if you add e. g. this it won't work anymore to click through to layer 1:
// layer2.setStyle( "-fx-background-color:yellow");
topLayer.setPickOnBounds(false);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();

}

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

另一个示例是将监听器添加到父级,例如。 G。场景并检查事件的目标,如下所示:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class LayersWithMouseEvents2 extends Application {


@Override
public void start(Stage primaryStage) {

Rectangle rectangle = new Rectangle( 100,100,100,100);
rectangle.setFill(Color.GREEN);

HBox hBox = new HBox();
hBox.setPrefSize(200, 200);
hBox.setStyle("-fx-background-color:yellow");
hBox.getChildren().add( rectangle);

Group root = new Group();
root.getChildren().add(hBox);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();

primaryStage.getScene().setOnMousePressed(e -> System.out.println("Scene: " + e));
}

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

根据您点击的位置,您会得到:

Scene: MouseEvent [source = javafx.scene.Scene@2c69fd61, target = javafx.scene.Scene@2c69fd61, eventType = MOUSE_PRESSED, consumed = false, x = 295.0, y = 134.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = null, point = Point3D [x = 295.0, y = 134.0, z = 0.0], distance = 1119.6152422706632]
Scene: MouseEvent [source = javafx.scene.Scene@2c69fd61, target = HBox@5a40dda8, eventType = MOUSE_PRESSED, consumed = false, x = 148.0, y = 134.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = HBox@5a40dda8, point = Point3D [x = 148.0, y = 134.0, z = 0.0], distance = 1119.6152422706632]
Scene: MouseEvent [source = javafx.scene.Scene@2c69fd61, target = Rectangle[x=100.0, y=100.0, width=100.0, height=100.0, fill=0x008000ff], eventType = MOUSE_PRESSED, consumed = false, x = 52.0, y = 54.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = Rectangle[x=100.0, y=100.0, width=100.0, height=100.0, fill=0x008000ff], point = Point3D [x = 152.0, y = 154.0, z = 0.0], distance = 1119.6152422706632]

随着可见性切换,它可能是这样的:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class LayersWithMouseEvents2 extends Application {

boolean hBoxVisible = true;

@Override
public void start(Stage primaryStage) {


Rectangle rectangle = new Rectangle( 100,100,100,100);
rectangle.setFill(Color.GREEN);

HBox hBox = new HBox();
hBox.setPrefSize(200, 200);
hBox.setStyle("-fx-background-color:yellow");
hBox.getChildren().add( rectangle);

Group root = new Group();
root.getChildren().add(hBox);

Scene scene = new Scene(root, 800, 600);

scene.setOnMousePressed(e -> {

System.out.println("Scene: " + e);

if( e.getTarget() == hBox) {
System.out.println( "HBox clicked");
}

if( e.getTarget() == rectangle) {
System.out.println( "Rectangle clicked");
}

if( e.getTarget() == scene) {
System.out.println( "Scene clicked");

hBoxVisible = !hBoxVisible;

hBox.setVisible(hBoxVisible);}

});

primaryStage.setScene( scene);
primaryStage.show();

}

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

关于JavaFx:检查鼠标是否位于节点的子节点上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837780/

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