JavaFX:网格窗格和列约束

JavaFX:网格窗格和列约束,javafx,javafx-8,fxml,scrollpane,gridpane,Javafx,Javafx 8,Fxml,Scrollpane,Gridpane,我有一个滚动窗格控件,里面有左右两部分。左侧部分包含一个网格窗格控件。我想在父控件的整个宽度上创建网格窗格 为此,我使用了列约束。现在,GridPane是全宽的,但是滚动被破坏了如何修复它? 请尝试使用和不使用columnConstraints的以下示例,您将了解我的意思 sample.FXML: 左内容 正确内容 使用相应的Controller.java: 公共类控制器实现可初始化{ @FXML 私有网格窗格; 私有字符串[]拆分文本; @凌驾 公共void初始化(URL位置,Resou

我有一个滚动窗格控件,里面有左右两部分。左侧部分包含一个网格窗格控件。我想在父控件的整个宽度上创建网格窗格

为此,我使用了列约束。现在,GridPane是全宽的,但是滚动被破坏了如何修复它?

请尝试使用和不使用columnConstraints的以下示例,您将了解我的意思


sample.FXML:


左内容
正确内容
使用相应的Controller.java:

公共类控制器实现可初始化{
@FXML
私有网格窗格;
私有字符串[]拆分文本;
@凌驾
公共void初始化(URL位置,ResourceBundle资源){
//来自维基百科
final String text=“海豚是一种分布广泛且多样的水生哺乳动物。它们是鲸目动物中的一个非正式类群,不包括鲸鱼和鼠海豚,因此动物学家认为这种类群是副系的。海豚包括现存的海豚科(海洋海豚)、扁担海豚科(印度河海豚),新世界河海豚科(新世界河海豚)和半咸水海豚科(半咸水海豚)。现存的海豚物种有40种。海豚与其他鲸目动物一样,属于有偶数趾有蹄动物的鲸目动物分支。”;
splittedText=text.split(“”);
对于(int i=0;i<20;++i){
add(新文本(Integer.toString(i)),0,i);
add(createFlowPane(),1,i);
}
}
私有FlowPane createFlowPane(){
FlowPane FlowPane=新的FlowPane();
对于(int i=0;i
您的意思是滚动窗格对内容没有反应?(滚动条不增长)?如何调用将其内容调整为视口宽度和高度的
滚动窗格
StackPane
是为了奢侈吗?@BoHalim完全正确。@大卫,我不是想告诉你这个。如果使内容的两个方向都适合视口,则不需要
滚动窗格
,因为这将确保没有可见的
滚动条
(当然,前提是内容的大小可以调整)。可能应该删除
fitToHeight=“true”
,或者将其替换为
fitToHeight=“false”
@fabian,我也考虑过这一点,但我尝试将这两种方法(
fitToHeight/fitToWidth
)都保持为(true),并且成功了(在
HBox
中的
节点
达到水平线上的最小宽度后),这很奇怪,因为
窗格
不会工作,但我认为一些内置布局窗格(
HBox/VBox
)忽略了这两种方法!你的意思是滚动窗格不会对内容做出反应?(滚动条不会增长)?如何调用将其内容大小调整为视口宽度和高度的
滚动窗格
StackPane
,对于奢侈的人来说?@BoHalim正是如此。@David这不是我想告诉你的。如果你使内容的两个方向都适合视口,你就不需要
滚动窗格
,因为这样可以确保没有可见的
滚动条(当然前提是内容可以调整大小)。可能
fitToHeight=“true”
应该被删除或替换为
fitToHeight=“false”
@fabian,我也考虑过这一点,但我尝试将这两种方法(
fitToHeight/fitToWidth
)都保持为(true),并且成功了(在
HBox
中的
节点
达到水平线上的最小宽度后),这很奇怪,因为
窗格
不起作用,但我认为一些内置布局窗格(
HBox/VBox
)忽略了这两种方法!
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.Text?>

<ScrollPane fx:controller="sample.Controller"
            xmlns:fx="http://javafx.com/fxml"
            fitToWidth="true"
            fitToHeight="true">
    <HBox>
        <AnchorPane fx:id="leftPane"
                    minWidth="500"
                    prefWidth="500"
                    maxWidth="500"
                    style="-fx-background-color: orange">
            <VBox style="-fx-background-color: lime"
                  AnchorPane.topAnchor="0"
                  AnchorPane.leftAnchor="0"
                  AnchorPane.rightAnchor="0"
                  AnchorPane.bottomAnchor="10">
                <GridPane fx:id="gridPane"
                          style="-fx-background-color: brown">
                    <columnConstraints>
                        <ColumnConstraints />
                        <!-- This causes the trouble! -->
                        <ColumnConstraints hgrow="ALWAYS" />
                    </columnConstraints>
                </GridPane>
                <Label>Left Content</Label>
            </VBox>
        </AnchorPane>
        <AnchorPane fx:id="rightPane"
                    HBox.hgrow="ALWAYS"
                    style="-fx-background-color: purple">
            <Text>Right Content</Text>
        </AnchorPane>
    </HBox>
</ScrollPane>
    public class Controller implements Initializable {
    @FXML
    private GridPane gridPane;
    private String[] splittedText;

    @Override
    public void initialize(URL location, ResourceBundle resources) {    
        // From Wikipedia
        final String text = "Dolphins are a widely distributed and diverse group of aquatic mammals. They are an informal grouping within the order Cetacea, excluding whales and porpoises, so to zoologists the grouping is paraphyletic. The dolphins comprise the extant families Delphinidae (the oceanic dolphins), Platanistidae (the Indian river dolphins), Iniidae (the new world river dolphins), and Pontoporiidae (the brackish dolphins). There are 40 extant species of dolphins. Dolphins, alongside other cetaceans, belong to the clade Cetartiodactyla with even-toed ungulates.";
        splittedText = text.split(" ");

        for (int i = 0; i < 20; ++i) {
            gridPane.add(new Text(Integer.toString(i)), 0, i);
            gridPane.add(createFlowPane(), 1, i);
        }
    }

    private FlowPane createFlowPane() {
        FlowPane flowPane = new FlowPane();
        for (int i = 0; i < splittedText.length; ++i)
            flowPane.getChildren().add(new Text(splittedText[i]));

        return flowPane;
    }
}