java - 如何防止 JavaFX 中的窗口太小?

标签 java javafx fxml scenebuilder

我有这个非常简单的表单,我将所有 UI 控件的最小宽度和高度设置为 USE_PREF_SIZE,因此它们不能太小:

enter image description here

代码如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <GridPane hgap="10.0" layoutX="64.0" layoutY="122.0" vgap="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints halignment="RIGHT" hgrow="NEVER" />
          <ColumnConstraints hgrow="SOMETIMES" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints vgrow="NEVER" />
          <RowConstraints vgrow="NEVER" />
          <RowConstraints vgrow="NEVER" />
            <RowConstraints valignment="TOP" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label minHeight="-Infinity" minWidth="-Infinity" text="Log in" GridPane.columnSpan="2147483647" GridPane.halignment="CENTER" />
            <Label minHeight="-Infinity" minWidth="-Infinity" text="Email address:" GridPane.rowIndex="1" />
            <Label minHeight="-Infinity" minWidth="-Infinity" text="Password:" GridPane.rowIndex="2" />
            <Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" text="Log in" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="3" />
            <PasswordField minHeight="-Infinity" minWidth="-Infinity" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField minHeight="-Infinity" minWidth="-Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1" />
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </GridPane>
   </children>
</AnchorPane>

我的问题是窗口仍然会变得太小:

enter image description here

如何防止这种情况发生?

我当然可以设置一个固定的最小宽度和高度,这很简单:

stage.setMinWidth(280);
stage.setMinHeight(180);

但这需要记住在每次 UI 更改时更新它,并且在其他具有较长字符串的语言(例如西类牙语)中它是不够的:

enter image description here

由于这似乎无法通过 FXML 实现,我正在尝试使用代码。我开始使用的基本代码是常用的 FXML 加载方法:

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/simple_form.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

FXML 加载对象 root 具有看起来很有前途的 prefWidth 和 prefHeight 属性。我添加了这个调试打印:

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/simple_form.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    System.out.println("Preferred width before showing: " + root.prefWidth(-1));
    System.out.println("Preferred height before showing: " + root.prefHeight(-1));
    stage.show();
    System.out.println("Preferred width after showing: " + root.prefWidth(-1));
    System.out.println("Preferred height after showing: " + root.prefHeight(-1));
    System.out.println("Actual width: " + scene.getWidth());
    System.out.println("Actual height: " + scene.getHeight());
}

结果是:

Preferred width before showing: 30.0
Preferred height before showing: 50.0
Preferred width after showing: 255.0
Preferred height after showing: 142.0
Actual width: 255.0
Actual height: 142.0

由于初始大小是首选大小,我想要的是此 UI 的最小大小,明智的人会期望得到答案(您只需要 show() 阶段第一):

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/simple_form.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    stage.setMinWidth(root.prefWidth(-1));
    stage.setMinHeight(root.prefHeight(-1));
}

但是这段代码的结果是我仍然可以让窗口变小,但只能变小一点:

enter image description here

我对这个结果感到非常困惑。我尝试从 fxml 中删除填充和间隙,但奇怪的行为仍然存在。

最佳答案

调用 Stage.sizeToScene(),然后在显示适合我的舞台后将最小宽度和高度设置为当前宽度和高度:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Login extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(FXMLLoader.load(getClass().getResource("LoginForm.fxml")));
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
        primaryStage.setMinWidth(primaryStage.getWidth());
        primaryStage.setMinHeight(primaryStage.getHeight());
    }

    public static void main(String[] args) {
        launch(args);
    }
}

sizeToScene() 在这里是多余的,因为这是这种情况下的默认行为,但如果您稍后更换舞台的场景,您可能需要它(并且使事情明确化从来都不是坏事)。

如果您想将舞台的大小明确设置为允许场景成为其首选大小的其他大小,但仍强制执行相同的最小大小要求,则需要一些技巧。展示舞台后,可以计算舞台的大小与场景根的大小(本质上是窗饰所占的空间)的差值,计算根的首选大小,然后使用那些计算舞台的最小尺寸。这看起来像

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Login extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(FXMLLoader.load(getClass().getResource("LoginForm.fxml")));


        primaryStage.setScene(scene);

        primaryStage.setWidth(400);
        primaryStage.setHeight(280);

        primaryStage.show();


        Node root = scene.getRoot();
        Bounds rootBounds = root.getBoundsInLocal();
        double deltaW = primaryStage.getWidth() - rootBounds.getWidth();
        double deltaH = primaryStage.getHeight() - rootBounds.getHeight();

        Bounds prefBounds = getPrefBounds(root);

        primaryStage.setMinWidth(prefBounds.getWidth() + deltaW);
        primaryStage.setMinHeight(prefBounds.getHeight() + deltaH);
    }

    private Bounds getPrefBounds(Node node) {
        double prefWidth ;
        double prefHeight ;

        Orientation bias = node.getContentBias();
        if (bias == Orientation.HORIZONTAL) {
            prefWidth = node.prefWidth(-1);
            prefHeight = node.prefHeight(prefWidth);
        } else if (bias == Orientation.VERTICAL) {
            prefHeight = node.prefHeight(-1);
            prefWidth = node.prefWidth(prefHeight);
        } else {
            prefWidth = node.prefWidth(-1);
            prefHeight = node.prefHeight(-1);
        }
        return new BoundingBox(0, 0, prefWidth, prefHeight);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我还没有在所有平台上测试过。

关于java - 如何防止 JavaFX 中的窗口太小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45905053/

相关文章:

java - JFrame 的 pack() 方法不起作用(java,ubuntu)

java - 如何使用 Navigator 在 Vaadin 中管理 uri

java - 我的过渡不起作用

JavaFX 将按钮从 Controller 获取到另一个类

java - 如何创建弹出消息来提醒用户字段不完整?

java - 如何将测试生成的源保留在生成的源目标文件夹之外

java - 使 JFace TableViewer 调整其周围复合 Material 的大小?

java - CalendarFX 结合 JavaFX : Module javafx. 未找到控件

java - 将时间线或任务绑定(bind)到进度条?

JavaFX:LoadException 解析按钮事件处理程序的 onAction