问题描述
我有一个以 ScrollPane
作为中心元素的 BorderPane
.它会自动调整大小以填满屏幕.ScrollPane
内部是一个 HBox
,它没有内容,但是是一个拖放目标.因此,我希望它填充其父 ScrollPane
.
I have a BorderPane
with a ScrollPane
as the center element. It resizes automatically to fill the screen. Inside the ScrollPane
is a HBox
that has no content but is a drag-and-drop target. Therefore I want it to fill its parent ScrollPane
.
最好的方法是什么?
到目前为止,我尝试的是重写 ScrollPane.resize(...)
方法来调整 HBox
的大小,但这似乎相当复杂且不合常规.
What I have tried so far is overriding the ScrollPane.resize(...)
method to resize the HBox
but it seems rather complicated and unorthodox.
要添加一些有用的代码,这是我可以解决问题的方法,但我相信必须有一些更好的方法来做到这一点:
edit: To add some useful code to this, this is how I can workaround the problem, but I believe there has to be some better way to do this:
@Override
public void resize(double width, double height) {
super.resize(width, height);
double scrollBarHeight = 2;
double scrollBarWidth = 2;
for (final Node node : lookupAll(".scroll-bar")) {
if (node instanceof ScrollBar) {
ScrollBar sb = (ScrollBar) node;
if (sb.getOrientation() == Orientation.HORIZONTAL) {
System.out.println("horizontal scrollbar visible = " + sb.isVisible());
System.out.println("width = " + sb.getWidth());
System.out.println("height = " + sb.getHeight());
if(sb.isVisible()){
scrollBarHeight = sb.getHeight();
}
}
if (sb.getOrientation() == Orientation.VERTICAL) {
System.out.println("vertical scrollbar visible = " + sb.isVisible());
System.out.println("width = " + sb.getWidth());
System.out.println("height = " + sb.getHeight());
if(sb.isVisible()){
scrollBarWidth = sb.getWidth();
}
}
}
}
hBox.setPrefSize(width-scrollBarWidth, height-scrollBarHeight);
}
部分代码取自这里:如何访问滚动条滚动窗格
推荐答案
试试
ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
不覆盖 resize()
方法.
这篇关于如何调整 JavaFX ScrollPane 内容的大小以适合当前大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!