我正在使用两个彼此重叠的SubScenes实现一个应用程序。第一个以3d形式保存节点,第二个ui组件(2d形状)。 Ui工作正常,但是例如使用“ setOnMouseClicked”时,无法到达3d子场景中的节点。
我的代码如下例:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
final double width = 500.0;
final double height = 500.0;
// init scene
Group root = new Group();
Scene scene = new Scene(root, width, height);
scene.setFill(Color.BLACK);
root.setOnMouseClicked(clicked -> System.out.println("I'm root!"));
scene.setOnMouseClicked(clicked -> System.out.println("I'm scene!"));
// init sub scene 'red'
AnchorPane redRoot = new AnchorPane();
SubScene red = new SubScene(redRoot, width, height);
Rectangle redRec = new Rectangle(100, 100, Color.RED);
AnchorPane.setTopAnchor(redRec, 100.0);
AnchorPane.setLeftAnchor(redRec, 100.0);
redRoot.getChildren().add(redRec);
red.setOnMouseClicked(clicked -> System.out.println("I'm red!"));
redRoot.setOnMouseClicked(clicked -> System.out.println("I'm red root!"));
redRec.setOnMouseClicked(clicked -> System.out.println("I'm red rec!"));
// init sub scene 'blue'
AnchorPane blueRoot = new AnchorPane();
SubScene blue = new SubScene(blueRoot, width, height);
Rectangle blueRec = new Rectangle(100, 100, Color.BLUE);
AnchorPane.setBottomAnchor(blueRec, 100.0);
AnchorPane.setRightAnchor(blueRec, 100.0);
blueRoot.getChildren().add(blueRec);
blue.setOnMouseClicked(clicked -> System.out.println("I'm blue!"));
blueRoot.setOnMouseClicked(clicked -> System.out.println("I'm blue root!"));
blueRec.setOnMouseClicked(clicked -> System.out.println("I'm blue rec!"));
// add sub scenes
root.getChildren().addAll(red, blue);
//root.getChildren().addAll(blue, red);
// init stage
primaryStage.setScene(scene);
primaryStage.setTitle("Subscene test");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
运行此示例并单击红色矩形输出:
I'm blue root!
I'm blue!
I'm root!
I'm scene!
单击蓝色将输出预期的结果:
I'm blue rec!
I'm blue root!
I'm blue!
I'm root!
I'm scene!
为什么事件分发链没有到达红色矩形?
感谢您的帮助!
关于, 鬃毛