JavaFX:如何在中心设置 ScrollPane 的滚动条?

JavaFX: How to set you scrollbars of a ScrollPane in the center?

我有一个扩展 Scrollpane 的 class。 我在滚动窗格中放了一个 4000 x 4000(高 x 宽)的窗格。 我希望我的滚动条居中,所以在 2000 x 2000 上。所以如果我 运行 我的 GUI,我会看到我放在 2000 x 2000 上的任何东西 这可能吗?

提前致谢!

您可以设置滚动位置:

scrollPane.setVvalue(2000);
scrollPane.setHvalue(2000);

看这里: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ScrollPane.html#setVvalue-double-

您可以在 ViewPort Bounds 的帮助下计算要滚动到的实际位置:

Bounds bounds = scrollPane.getViewportBounds();
scrollPane.setHvalue(2000 - bounds.getWidth()/2);
scrollPane.setVvalue(2000 - bounds.getHeight()/2);

我找到了解决方案:

scrollPane.setVvalue(0.5);
scrollPane.setHvalue(0.5);

这将滚动条都设置在 4000 的中间。 该值必须介于 0 和 1 之间