我有一个包含ScrollPanel的fxml文件,用于在TabPanel中显示图像:
<Tab text="Example" fx:id="liveTab" fx:controller="me.example.ExampleController" xmlns:fx="http://javafx.com/fxml">
<content>
<HBox prefHeight="600.0" prefWidth="1200.0">
<children>
<FlowPane prefHeight="600.0" prefWidth="400.0">
<children>
<ScrollPane prefHeight="580.0" prefWidth="200.0">
<content>
<ListView fx:id="listView" prefHeight="580.0" prefWidth="190.0">
<items>
<FXCollections fx:factory="observableArrayList">
</FXCollections>
</items>
</ListView>
</content>
</ScrollPane>
<ScrollPane prefHeight="580.0" prefWidth="200.0">
<content>
<ListView prefHeight="580.0" prefWidth="190.0">
<items>
<FXCollections fx:factory="observableArrayList">
</FXCollections>
</items>
</ListView>
</content>
</ScrollPane>
<HBox alignment="BOTTOM_RIGHT" prefHeight="20.0" prefWidth="400.0" spacing="10.0" GridPane.columnIndex="0" GridPane.rowIndex="2">
<Button text="Add"/>
<Button text="Remove"/>
</HBox>
</children>
</FlowPane>
<VBox>
<children>
<ScrollPane fitToHeight="true" fitToWidth="true">
<content>
<ImageView fx:id="imageContainer" pickOnBounds="true" preserveRatio="true">
<Image url="file:/C:/Users/Public/Pictures/Sample%20Pictures/Koala.jpg" preserveRatio="true" />
</ImageView>
</content>
</ScrollPane>
</children>
</VBox>
</children>
</HBox>
</content>
</Tab>
我希望ImageView占用包含选项卡的所有剩余空间,我试图使用fitToHeight =“true”fitToWidth =“true”,并从FlowPane更改为VBox但我仍然得到这个:
答案 0 :(得分:1)
确实
<ScrollPane fx:id="scrollPane" fitToHeight="true" fitToWidth="true">
...
帮助?
答案 1 :(得分:0)
好的,我想出了如何获得理想的行为。使用AnchorPane可以强制子节点相对于父节点占用空间。我分享代码以防有人发现它有用:
<Tab text="Example" fx:id="liveTab" fx:controller="me.example.ExampleController" xmlns:fx="http://javafx.com/fxml">
<content>
<AnchorPane prefHeight="600.0" prefWidth="1200.0">
<children>
<FlowPane prefHeight="600.0" prefWidth="400.0">
<children>
<ScrollPane prefHeight="580.0" prefWidth="200.0">
<content>
<ListView fx:id="listView" prefHeight="580.0" prefWidth="190.0">
<items>
<FXCollections fx:factory="observableArrayList">
</FXCollections>
</items>
</ListView>
</content>
</ScrollPane>
<ScrollPane prefHeight="580.0" prefWidth="200.0">
<content>
<ListView prefHeight="580.0" prefWidth="190.0">
<items>
<FXCollections fx:factory="observableArrayList">
</FXCollections>
</items>
</ListView>
</content>
</ScrollPane>
<HBox alignment="BOTTOM_RIGHT" prefHeight="20.0" prefWidth="400.0" spacing="10.0" GridPane.columnIndex="0" GridPane.rowIndex="2">
<Button text="Add"/>
<Button text="Remove"/>
</HBox>
</children>
</FlowPane>
<AnchorPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="400.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<ScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<ImageView fx:id="imageContainer" pickOnBounds="true" preserveRatio="true">
<Image url="file:/C:/Users/Public/Pictures/Sample%20Pictures/Koala.jpg" preserveRatio="true" />
</ImageView>
</content>
</ScrollPane>
</AnchorPane>
</children>
</AnchorPane>
</content>
</Tab>
结果: