IMO 使用StackPane 进行顶部布局更合适:
@Override
public void start( Stage stage )
{
Button b = new Button( "Close" );
Label l = new Label( "Console" );
StackPane sp = new StackPane( l, b );
StackPane.setAlignment( l, Pos.CENTER_LEFT );
StackPane.setAlignment( b, Pos.CENTER_RIGHT );
TextArea area = new TextArea();
VBox.setVgrow( area, Priority.ALWAYS );
VBox box = new VBox( sp, area );
Scene scene = new Scene( box, 800, 600 );
stage.setScene( scene );
stage.show();
}
但是HBox的使用是不可避免的,添加另一个元素作为间隔:
@Override
public void start( Stage stage )
{
Button b = new Button( "Close" );
Label l = new Label( "Console" );
Pane spacer = new Pane();
HBox hBox = new HBox(l, spacer, b);
HBox.setHgrow( spacer, Priority.ALWAYS);
TextArea area = new TextArea();
VBox.setVgrow( area, Priority.ALWAYS );
VBox box = new VBox( hBox, area );
Scene scene = new Scene( box, 800, 600 );
stage.setScene( scene );
stage.show();
}