java 按钮绝对位置_java – 按钮的绝对布局 – 居中

最简单的方法是使用VBox:

public void start(final Stage stage) throws Exception {

final Button button0 = new Button("Start learning");

final Button button1 = new Button("Customize");

final VBox box = new VBox();

box.setFillWidth(true);

box.getChildren().setAll(button0, button1);

box.setAlignment(Pos.CENTER);

stage.setScene(new Scene(box));

stage.setWidth(200);

stage.setHeight(100);

stage.show();

}

另一种可能的方法是使用GridPane:

public void start(final Stage stage) throws Exception {

final Button button0 = new Button("Start learning");

final Button button1 = new Button("Customize");

final GridPane cPane = new GridPane();

cPane.getChildren().addAll(button0, button1);

GridPane.setConstraints(button0, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER);

GridPane.setConstraints(button1, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER);

final ColumnConstraints columnn0 = new ColumnConstraints();

columnn0.setPercentWidth(100);

cPane.getColumnConstraints().addAll(columnn0);

final RowConstraints row0 = new RowConstraints(1);

row0.setPercentHeight(50);

final RowConstraints row1 = new RowConstraints(1);

row1.setPercentHeight(50);

cPane.getRowConstraints().addAll(row0, row1);

stage.setScene(new Scene(cPane));

stage.setWidth(200);

stage.setHeight(100);

stage.show();

}

这里的想法是配置网格中的行和列,以使用相应的约束对象填充场景.上面定义了一列和两行.然后,您可以使用GridPane.setConstraints(…)在Grid的单元格内对齐组件.

您可能需要稍微更改代码以使顶部按钮与VPos.BOTTOM对齐,将较低的按钮与VPos.TOP对齐,具体取决于您是否希望按钮粘在一起(然后您必须为两者定义边距,当然).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值