我有4根柱子(固定1,液体,固定2,液体)。每个液柱必须具有宽度=(gridpane width-fixed1-fixed2)/ 2。换句话说,液体宽度的50%。重要提示:所有宽度=液体宽度+固定宽度。为了设置列约束,我找到了以下代码:
GridPane gridpane = new GridPane();
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(25);
gridpane.getColumnConstraints().addAll(col1);
但是,此代码设置的不是液体宽度的百分比,而是所有宽度的百分比。怎么做?
答案 0 :(得分:3)
试试这个;
@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();
}
通过调整窗口大小来测试占用的宽度。可用空间在Priority.ALWAYS
增长的列之间平均共享。其中有2个,每个50%。因此,它分发为:
col-1: fixed 60px
col-2: floated 50%
col-3: fixed 100px
col-4: floated 50%