如何在JavaFX中检测选项卡头的位置和大小

我还是javaFX的新手。 在我的代码中,我需要获取选项卡标题的位置和大小,但我找不到任何返回其大小或位置的属性或函数。

我没有正确理解你的问题,但通常你可以使用lookup函数来获取.tab-header-area CSS选择器 ,它实际上是一个StackPane

 TabPane tabPane = new TabPane(); tabPane.getTabs().addAll(new Tab("Tab1"), new Tab("Tab2"), new Tab("Tab3")); Button b = new Button("Get header"); b.setOnAction((e) -> { StackPane headerArea = (StackPane) tabPane.lookup(".tab-header-area"); System.out.println("Coordinates relatively to Scene: " + headerArea.localToScene(headerArea.getBoundsInLocal())); }); 

输出

 Coordinates relatively to Scene: BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:500.0, height:29.0, depth:0.0, maxX:500.0, maxY:29.0, maxZ:0.0] 

如果要获取有关各个选项卡的信息,请执行以下操作:

 Set tabs = tabPane.lookupAll(".tab"); for (Node node : tabs) { System.out.println("Coordinates relatively to Scene: " + node.localToScene(node.getBoundsInLocal())); } 

和输出

 Coordinates relatively to Scene: BoundingBox [minX:5.0, minY:5.0, minZ:0.0, width:54.0, height:24.0, depth:0.0, maxX:59.0, maxY:29.0, maxZ:0.0] Coordinates relatively to Scene: BoundingBox [minX:59.0, minY:5.0, minZ:0.0, width:38.0, height:24.0, depth:0.0, maxX:97.0, maxY:29.0, maxZ:0.0] Coordinates relatively to Scene: BoundingBox [minX:97.0, minY:5.0, minZ:0.0, width:38.0, height:24.0, depth:0.0, maxX:135.0, maxY:29.0, maxZ:0.0] 

我使用了localToScene函数和StackPane 局部边界的参数来获取相对于Scene的坐标。

注意:在将CSS应用于Scene之前,不保证查找有效。