如何防止 window 在 JavaFX 中过小?

How can I prevent a window from being too small in JavaFX?

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

代码如下所示:

<?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>

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

如何防止这种情况发生?

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

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

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

由于这似乎无法通过 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));
}

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

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

您应该通过以下方式阻止 window 调整大小:

primaryStage.setResizable(false);

调用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() 在这里是多余的,因为这是这种情况下的默认行为,但如果您稍后更换舞台的场景,您可能需要它(并且让事情变得明确从来都不是坏事) .

如果您想将舞台的大小明确设置为允许场景成为其首选大小的其他大小,但仍强制执行相同的最小大小要求,则有点棘手。展示舞台后,可以计算舞台尺寸与场景根部尺寸的差值(本质上就是window装饰所占的space),计算出首选尺寸的根,然后使用它们来计算舞台的最小尺寸。这看起来像

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

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

基于,我编写了这两个函数。在我的应用程序中,我在 Stage.show():

之后调用 applyRootSizeContraints
private static Bounds getBounds(final Node node,
                                final BiFunction<Node, Double, Double> widthFunction,
                                final BiFunction<Node, Double, Double> heightFunction) {
    final Orientation bias = node.getContentBias();
    double prefWidth;
    double prefHeight;
    if (bias == Orientation.HORIZONTAL) {
        prefWidth = widthFunction.apply(node, (double) -1);
        prefHeight = heightFunction.apply(node, prefWidth);
    } else if (bias == Orientation.VERTICAL) {
        prefHeight = heightFunction.apply(node, (double) -1);
        prefWidth = widthFunction.apply(node, prefHeight);
    } else {
        prefWidth = widthFunction.apply(node, (double) -1);
        prefHeight = heightFunction.apply(node, (double) -1);
    }
    return new BoundingBox(0, 0, prefWidth, prefHeight);
}

private static void applyRootSizeConstraints(final Stage stage) {
    final Parent root = stage.getScene().getRoot();
    stage.sizeToScene();
    final double deltaWidth = stage.getWidth() - root.getLayoutBounds().getWidth();
    final double deltaHeight = stage.getHeight() - root.getLayoutBounds().getHeight();
    final Bounds minBounds = getBounds(root, Node::minWidth, Node::minHeight);
    stage.setMinWidth(minBounds.getWidth() + deltaWidth);
    stage.setMinHeight(minBounds.getHeight() + deltaHeight);
    final Bounds prefBounds = getBounds(root, Node::prefWidth, Node::prefHeight);
    stage.setWidth(prefBounds.getWidth() + deltaWidth);
    stage.setHeight(prefBounds.getHeight() + deltaHeight);
    final Bounds maxBounds = getBounds(root, Node::maxWidth, Node::maxHeight);
    stage.setMaxWidth(maxBounds.getWidth() + deltaWidth);
    stage.setMaxHeight(maxBounds.getHeight() + deltaHeight);
}

另请注意,我使用了 Node.getLayoutBounds instead of Node.getBoundsInLocal