如何检测 JavaFX 中选项卡标题的位置和大小

Ami*_*til 3 java javafx javafx-8

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

DVa*_*rga 5

我没有正确理解你的问题,但通常你可以使用查找功能来获取.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()));
});
Run Code Online (Sandbox Code Playgroud)

输出

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]
Run Code Online (Sandbox Code Playgroud)

如果要获取有关各个选项卡的信息:

Set<Node> tabs = tabPane.lookupAll(".tab");
for (Node node : tabs) {
    System.out.println("Coordinates relatively to Scene: " + node.localToScene(node.getBoundsInLocal()));
}
Run Code Online (Sandbox Code Playgroud)

和输出

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]
Run Code Online (Sandbox Code Playgroud)

我已经使用了localToScene功能与的参数本地范围StackPane获得相对坐标Scene

注意:在将 CSS 应用于Scene.