JavaFx:液体和固定网格窗格列宽

JavaFx: liquid and fixed gridpane column widths

我的网格窗格有 4 列(固定 1、液体、固定 2、液体)。每个液体列的宽度必须= (gridpane width-fixed1-fixed2)/2。换句话说,液体宽度的 50%。重要:所有宽度=液体宽度+固定宽度。为了设置列约束,我找到了以下代码:

GridPane gridpane = new GridPane();
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(25);
gridpane.getColumnConstraints().addAll(col1);

但是,此代码设置的不是液体宽度的百分比,而是所有宽度的百分比。怎么做?

试试这个;

@Override
public void start( final Stage primaryStage )
{

    ColumnConstraints col1 = new ColumnConstraints();
    col1.setHgrow( Priority.ALWAYS );

    ColumnConstraints col2 = new ColumnConstraints();
    col2.setHgrow( Priority.ALWAYS );

    GridPane gridPane = new GridPane();
    gridPane.setGridLinesVisible( true );
    gridPane.getColumnConstraints().addAll( new ColumnConstraints( 60 ), col1, new ColumnConstraints( 100 ), col2 );

    gridPane.addColumn( 0, new Button( "col 1" ) );
    gridPane.addColumn( 1, new Button( "col 2" ) );
    gridPane.addColumn( 2, new Button( "col 3" ) );
    gridPane.addColumn( 3, new Button( "col 4" ) );

    final Scene scene = new Scene( new VBox( gridPane ), 400, 300 );
    primaryStage.setScene( scene );
    primaryStage.show();
}

通过调整 window 的大小来测试占用的宽度。免费 space 由 Priority.ALWAYS 增长的列平均共享。有 2 个,所以每个 50%。结果它被分配为:

col-1: fixed 60px
col-2: floated 50%
col-3: fixed 100px
col-4: floated 50%