Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 填充窗格的宽度_Java_Layout_Javafx - Fatal编程技术网

Java 填充窗格的宽度

Java 填充窗格的宽度,java,layout,javafx,Java,Layout,Javafx,有一个窗格作为根,一个网格窗格作为其子级。根将随阶段调整大小。如何实现网格窗格自动调整根的大小以填充宽度任何宽度都应该是可能的。在没有属性绑定的情况下是否可能 情况就是这样: 窗格根(黄色)是场景的根,因此它填充了阶段的宽度和高度 一个网格窗格(绿色),其中一行和三列作为根的子项 行应具有固定的高度 第一列和第三列的大小应保持不变 中间的列应该增长到最大值 GridPane应填充其父窗格的宽度 不幸的是,GridPane没有填充宽度: 代码: 实际上,您的结果是预期的100+200+100=4

有一个
窗格
作为根,一个
网格窗格
作为其子级。根将随阶段调整大小。如何实现
网格窗格
自动调整根的大小以填充宽度任何宽度都应该是可能的。在没有属性绑定的情况下是否可能

情况就是这样:

  • 窗格
    (黄色)是
    场景
    的根,因此它填充了
    阶段
    的宽度和高度
  • 一个
    网格窗格
    (绿色),其中一行和三列作为根的子项
  • 行应具有固定的高度
  • 第一列和第三列的大小应保持不变
  • 中间的列应该增长到最大值
  • GridPane
    应填充其父窗格的宽度
  • 不幸的是,
    GridPane
    没有填充宽度:

    代码:


    实际上,您的结果是预期的
    100+200+100=400px

    final ColumnConstraints col1 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
    //                                                   ↑
    final ColumnConstraints col2 = new ColumnConstraints(200, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
    //                                                   ↑
    final ColumnConstraints col3 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
    //                                                   ↑
    
    你定义:

    primaryStage.setWidth(600);
    
    只需更改“中”列以填充剩余的
    200px

    final ColumnConstraints col1 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
    final ColumnConstraints col2 = new ColumnConstraints(400, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
    //                                                   ↑ change to 400
    final ColumnConstraints col3 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
    

    窗格内节点的大小由单个窗格实现其布局的方式控制:即,节点的大小有效地由其父节点确定。因此,网格窗格的总体大小由
    窗格
    的布局实现控制

    窗格
    实际上只做了最小的布局工作:它有效地将所有内容定位在(0,0)位置,并将其大小调整为首选大小。因此,网格窗格将获得其首选宽度,即列的首选宽度之和

    因此,要使网格窗格增长,您需要更改其首选宽度,或者使用允许您控制其增长方式的布局窗格

    例如:

    gridPane.prefWidthProperty().bind(root.widthProperty());
    
    将使网格窗格增长到根的宽度。这只会在所有三列的右侧添加额外的空间,因此需要额外设置

    col2.setHgrow(Priority.ALWAYS);
    
    将使
    col2
    具有额外的宽度

    或者,对
    根使用
    锚烷

    // Pane root = new Pane();
    AnchorPane root = new AnchorPane();
    
    和背景

    AnchorPane.setLeftAnchor(gridPane, 0.0);
    AnchorPane.setRightAnchor(gridPane, 0.0);
    
    (再次使用
    col2
    hgrow
    设置)将具有相同的效果

    或者你也可以

    VBox root = new VBox();
    root.setFillWidth(true);
    
    (例如:有许多解决方案)


    解决方案的选择取决于根窗格中的其他节点(如果有的话)。

    窗格
    不会自动布局其子节点。尝试改用
    VBox
    。它将通过(如果可能的话)调整其子对象的大小来填充可用空间的宽度。这样,网格窗格将在舞台宽度更改时调整大小

    现在,您可以管理gridpane列共享空间的方式。根据你的描述,这就足够了

    col2.setHgrow( Priority.ALWAYS );
    

    要使gridpane自动调整,请使用fxml。不要用java做这件事

    请参见下文,它们设置为百分比宽度。下面示例中的最后一行具有固定设置

    <GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    
      <columnConstraints>
        <ColumnConstraints percentWidth="50" /> 
        <ColumnConstraints percentWidth="50" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints percentHeight="35" />
        <RowConstraints percentHeight="35" />
        <RowConstraints maxHeight="106.0" minHeight="10.0" prefHeight="106.0" vgrow="ALWAYS" />
      </rowConstraints>
       <children>
    
    
    
    谢谢,这将是根的恒定宽度的答案。但我希望GridPane在宽度上动态增长。所以它应该适用于任何宽度。James_D速度更快,但+1:)
    <GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    
      <columnConstraints>
        <ColumnConstraints percentWidth="50" /> 
        <ColumnConstraints percentWidth="50" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints percentHeight="35" />
        <RowConstraints percentHeight="35" />
        <RowConstraints maxHeight="106.0" minHeight="10.0" prefHeight="106.0" vgrow="ALWAYS" />
      </rowConstraints>
       <children>