Len*_*ert 49 resize javafx scrollpane autolayout
我有一个BorderPane带ScrollPane为中心元素.它会自动调整大小以填充屏幕.里面ScrollPane是一个HBox没有内容但是是一个拖放目标.因此,我希望它填补其父母ScrollPane.
这样做的首选方法是什么?
到目前为止我所尝试的是重写ScrollPane.resize(...)调整大小的方法,HBox但它似乎相当复杂和非正统.
编辑:要为此添加一些有用的代码,这就是我可以解决问题的方法,但我相信必须有更好的方法来做到这一点:
@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);
}
Run Code Online (Sandbox Code Playgroud)
代码部分取自此处:如何访问ScrollPane的Scrollbars
Ulu*_*Biy 81
尝试
ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
Run Code Online (Sandbox Code Playgroud)
没有覆盖resize()方法.
NDY*_*NDY 22
通过或设置fitToHeight或fitToWidth属性:XMLCSS
FXML:
<ScrollPane fx:id="myScrollPane" fitToHeight="true" fitToWidth="true" />
Run Code Online (Sandbox Code Playgroud)
CSS:
.my-scroll-pane {
-fx-fit-to-height: true;
-fx-fit-to-width: true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43895 次 |
| 最近记录: |