我从 Javafx 开始,我对自动调整大小有疑问。我在一个带有一些文本标签和按钮的窗口中有一个空的网格窗格。窗口的大小与内容相匹配。在将子项添加到网格窗格时,我需要自动调整窗口大小,我认为我会在添加子项后调用sizeToScene来解决它,但我认为它不起作用,因为我在绘制之前调用它,所以它调整到高度空的网格窗格(所以它什么都不做)。有没有简单的方法来解决这个问题?谢谢!!
public class controller {
@FXML Button button_populate;
@FXML GridPane numbers_pane;
@FXML BorderPane root;
public controller(){
}
private void setNumberTable(int numberOfButtons){
int buttonNumber = 1;
int rows = (int) numberOfButtons/10;
for (int row = 0; row < rows; row++) {
for (int column = 0; column < 10 && buttonNumber <= 100; column++) {
Button button = new Button(String.valueOf(buttonNumber++));
numbers_pane.add(button, column, row);
}
}
Stage stage = (Stage) root.getScene().getWindow();
stage.sizeToScene();
}
@FXML
private void initialize() {
button_populate.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
setNumberTable(100);
}
});
}
}