【问题标题】:How to make layout's content resizable?如何使布局的内容可调整大小?
【发布时间】:2015-07-05 17:50:36
【问题描述】:

我已经在 scene builder 中创建了这个:

我的目标是让它更漂亮一点:

  • 问题 1:将关闭按钮放在 MAX RIGHT

  • 问题 2:粘贴不同的布局和 TextArea 到窗口边框,因为当我调整窗口大小时,组件不会移动。

为此,我想操作布局属性,但我真的不知道如何操作。 我正在考虑使用间距来解决我的问题一,但这似乎很复杂,我不知道如何解决我的第二个问题。

我的布局选择正确吗?我必须使用哪些属性来解决我的问题?

谢谢。

【问题讨论】:

  • 你能重新表述问题 2 吗?
  • @UlukBiy 完成,如果你需要,我可以添加图片。

标签: java layout javafx scenebuilder


【解决方案1】:

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();
}

【讨论】:

  • 有没有办法让你使用 StackPane 但使用广告 HBox ?
  • 您能否在回答中解释一下:“Vgrow,Hgrow”和不同的优先级是什么?很好的答案,谢谢。
  • 基本上,这些设置控制如何在父布局具有的子级之间共享额外的空白空间,例如调整舞台大小。有关更完整的解释,请阅读相关的 javadoc。也请查看 oracle 的布局教程:docs.oracle.com/javafx/2/layout/jfxpub-layout.htm
猜你喜欢
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多