JavaFX:滚动窗格不适合父级

mar*_*lin 5 java layout javafx

我想创建以下场景:

+-----------------------------------------------+
|ROOT (=BorderPane)                             |
|                                               |
| +-------------------------------------------+ |
| |TOOL BAR                                   | |
| |(north)                                    | |
| +-------------------------------------------+ |
|                                               |
| +-------------------------------------------+ |
| |MyConfigurationComponent extends BorderPane| |
| |(center)                                   | |
| |  +-------------------------+  +---------+ | |
| |  |ScrollPane               |  |         | | |
| |  |(center)                 |  |         | | |
| |  | +---------------------+ |  |         | | |
| |  | |                     | |  |         | | |
| |  | |                     | |  |  SIDE   | | |
| |  | |       CANVAS        | |  |  PANE   | | |
| |  | |                     | |  | (right) | | |
| |  | |                     | |  |         | | |
| |  | |                     | |  |         | | |
| |  | +---------------------+ |  |         | | |
| |  |                         |  |         | | |
| |  +-------------------------+  +---------+ | |
| |                                           | |
| +-------------------------------------------+ |
|                                               |
+-----------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

期望

我想ScrollPane尽可能大.这意味着ScrollPane无论场景的大小如何,都应该增长并填充整个区域.

既然TOOL BAR并且SIDE PANE具有固定的(优选的)宽度和高度,我认为ScrollPane将拉伸并填充整个剩余区域.

实际行为

但事实并非如此.在ScrollPane神奇地将其最小尺寸的东西约40×40.高度被拉伸以匹配高度SIDE PANEL,但宽度仍然相同,似乎没有任何东西可以改变它.

事实上,唯一一个使得ScrollPane更大的事情就是明确地将minWidth设置为某​​个值(即400).但这并没有解决有关ScrollPane汽车电阻的问题.

当我将背景颜色设置为时MyConfigurationComponent,我可以看到背景颜色按预期填充整个区域(所以问题不在于MyConfigurationComponent).看起来它的中心child(ScrollPane)呈现为left,右边的child(SIDE PANEL)作为中心,因为右侧的所有剩余区域都是空白的(但是背景,所以它仍然是MyConfigurationComponent!).

我试过了什么

  • 设置min,pref以及特定值的max大小ScrollPane
  • 将首选大小绑定到父级的大小
  • 设置fitToWidthfitToHeight为真
  • ScrollPaneAnchorPane四边包裹着锚
  • ScrollPane用VBox 包装并将优先级设置为allways

目前的解决方法

将宽度的MyConfigurationComponent宽度绑定SIDE PANELScrollPane.在很多方面都错了.

ROOT.fxml

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cz.upol.fapapp.cfa.gui.frame.FxCFAConfigFrameController">
   <top>
            <ToolBar>
                <Button text="Some button" />
            </ToolBar>
   </top>
   <center>
      <MyConfigurationComponent fx:id="configComp" />
   </center> 
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

MyConfigurationComponent.fxml

<BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <ScrollPane fx:id="scrollPane">
            <content>
                <Canvas width="2000" height="2000" />
            </content>
        </ScrollPane>
    </center>
    <right>
        <VBox prefWidth="100.0" BorderPane.alignment="CENTER_RIGHT">
            <children>
                <Label text="Colors" />
                <ComboBox fx:id="cmbColors" prefWidth="100.0" />    
                <!-- ... -->
                <Separator prefHeight="15.0" prefWidth="100.0" />
                <Button mnemonicParsing="false" text="Action 1" />
                <Button mnemonicParsing="false" text="Action 2" />
            </children>
        </VBox>
    </right>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

问题

  • BorderPane中心的规模扩大到填充内容,不是吗?
  • 这可能是由BorderPane内部造成的BorderPane吗?
  • 有没有比"我的解决方法"更好的解决方案?

Sup*_*man 0

您可以将其放入代码中的 start 方法中:

stage.widthProperty().addListener(event -> {
    scrollPane.setPrefWidth(stage.getWidth());
});

stage.heightProperty().addListener(event -> {
    scrollPane.setPrefHeight(stage.getHeight());
});
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,我建议也将 BorderPane 的宽度和高度设置为舞台的尺寸:

stage.widthProperty().addListener(event -> {
    scrollPane.setPrefWidth(stage.getWidth());
    borderPane.setPrefWidth(stage.getWidth());
});

stage.heightProperty().addListener(event -> {
    scrollPane.setPrefHeight(stage.getHeight());
    borderPane.setPrefHeight(stage.getHeight());
});
Run Code Online (Sandbox Code Playgroud)