我正在开发 JavaFX FXML 应用程序。我需要在运行时从 Controller 类调整窗口大小。
我发现可以通过设置舞台的 maxHeight 和 maxWidth 属性从应用程序类执行此操作。但是如何在应用程序运行时从 Controller 类中做到这一点呢?
最佳答案
在 Controller 类中定义一个按钮并设置它的 Action
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// OR, if you define btn as @FXML private Button btn.
Stage stage = (Stage) btn.getScene().getWindow();
// these two of them return the same stage
stage.setWidth(new_val);
stage.setHeight(new_val);
}
});
其中 stage 是您的主要(主要)阶段。
关于java - 如何在运行时设置窗口大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13342598/