I am trying to programatically insert a single image onto my application. The image does show up in its AnchorPane, but the problem is that the AnchorPane (and therefore the ImageView as well) appears to be stuck to the top left corner of the window.

This AnchorPane is a child of a ScrollPane. I want my image/AnchorPane to be aligned to the top center of the ScrollPane, not the top left. Is dynamic centering of an AnchorPane possible?

有帮助吗?

解决方案

To dynamically center an AnchorPane, place it in a StackPane.


To place the AnchorPane in the top center of the StackPane, call

StackPane.setAlignment(node, Pos.TOP_CENTER)

AnchorPane anchorPane = new AnchorPane();
...
StackPane stackPane = new StackPane();
stack.getChildren().add(anchorPane);
StackPane.setAlignment(anchorPane, Pos.TOP_CENTER);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top