我从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);
}
});
}
}