Java 组件的 FX 更改对齐
Java FX change Halignment of Component
public CheckBox createBox(){
CheckBox box = new CheckBox();
box.setText(name);
box.setOnMouseClicked(x -> handlePlaying(box));
return box;
}
比我将它们添加到 GridPane
checkBoxes
.forEach(x -> gridPane.add(x, checkBoxes.indexOf(x) + 1, 2));
现在想改变对齐方式,但是找不到方法。
PS: 我想更改对齐方式而不是一般对齐方式;
我相信这个例子说明了 Labelled.setAlignment() 和 GridPane.setHAlignment() 之间的区别。
代码
@Override
public void start(Stage primaryStage) throws Exception {
GridPane grid = new GridPane();
CheckBox box1 = new CheckBox("horse");
box1.setMinWidth(200);
box1.setAlignment(Pos.CENTER_RIGHT);
grid.getChildren().add(box1);
CheckBox box2 = new CheckBox("banana");
grid.getChildren().add(box2);
GridPane.setRowIndex(box2, 1);
GridPane.setHalignment(box2, HPos.RIGHT); // <=== this is probably what you want
primaryStage.setScene(new Scene(grid));
primaryStage.show();
}
我建议 FXML 而不是程序化布局,但同样的原则也适用。在 SceneBuilder 的布局选项卡中找到 HAlignment 属性...
Labelled.setAlignment() 确实已经在其 JavaDoc 中对此进行了解释,尽管我明白为什么这可能会造成混淆。
Specifies how the text and graphic within the Labeled should be
aligned when there is empty space within the Labeled.
public CheckBox createBox(){
CheckBox box = new CheckBox();
box.setText(name);
box.setOnMouseClicked(x -> handlePlaying(box));
return box;
}
比我将它们添加到 GridPane
checkBoxes
.forEach(x -> gridPane.add(x, checkBoxes.indexOf(x) + 1, 2));
现在想改变对齐方式,但是找不到方法。
PS: 我想更改对齐方式而不是一般对齐方式;
我相信这个例子说明了 Labelled.setAlignment() 和 GridPane.setHAlignment() 之间的区别。
代码
@Override
public void start(Stage primaryStage) throws Exception {
GridPane grid = new GridPane();
CheckBox box1 = new CheckBox("horse");
box1.setMinWidth(200);
box1.setAlignment(Pos.CENTER_RIGHT);
grid.getChildren().add(box1);
CheckBox box2 = new CheckBox("banana");
grid.getChildren().add(box2);
GridPane.setRowIndex(box2, 1);
GridPane.setHalignment(box2, HPos.RIGHT); // <=== this is probably what you want
primaryStage.setScene(new Scene(grid));
primaryStage.show();
}
我建议 FXML 而不是程序化布局,但同样的原则也适用。在 SceneBuilder 的布局选项卡中找到 HAlignment 属性...
Labelled.setAlignment() 确实已经在其 JavaDoc 中对此进行了解释,尽管我明白为什么这可能会造成混淆。
Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.