我在场景构建器中创建了这个:
我的目标是让它变得更漂亮一点:
问题1:将关闭按钮放在MAX RIGHT
问题 2:将不同的布局和 TextArea 粘贴到窗口边框,因为当我调整窗口大小时,组件不会移动。
为此,我想操作布局属性,但我真的不知道如何操作。 我正在考虑使用间距来解决我的问题一,但这似乎很复杂,而且我不知道如何解决我的第二个问题。
我选择的布局正确吗?我必须使用哪些属性来解决我的问题?
谢谢。
最佳答案
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();
}
关于java - 如何使布局的内容可调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892986/