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));
现在我想改变Halignment,但是找不到方法。
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 属性...
Labeled.setAlignment() 确实在其 JavaDoc 中解释了这一点,尽管我可以理解为什么这可能会令人困惑。
Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.
关于Javafx更改组件的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40833961/