2
我對javaFX還是個新手。在我的代碼中,我需要獲取標題標題的位置和大小,但是我找不到任何返回其大小或位置的屬性或函數。如何檢測JavaFX中標籤頁眉的位置和大小
我對javaFX還是個新手。在我的代碼中,我需要獲取標題標題的位置和大小,但是我找不到任何返回其大小或位置的屬性或函數。如何檢測JavaFX中標籤頁眉的位置和大小
我不明白你的問題,但通常你可以使用lookup函數來得到.tab-header-area CSS selector這實際上是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<Node> 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]
我已經使用的參數local bounds的localToScene函數來獲取相對於Scene
的座標。
注意:在CSS應用到Scene
之前,查找不能保證能正常工作。
只是想知道爲什麼你想要這些信息。也許有更好的方法去做你實際想做的事情。 –