【发布时间】:2018-02-04 21:36:18
【问题描述】:
我有这个非常简单的表单,我将所有 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>
我的问题是窗口仍然会变得太小:
如何防止这种情况发生?
我当然可以设置一个固定的最小宽度和高度,这很简单:
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 中删除填充和间隙,但奇怪的行为仍然存在。
【问题讨论】:
-
是否手动调整大小?然后我会说禁用手动调整大小。
-
是的,我手动将其调整为小尺寸。我不想禁用手动调整大小,因为有人可能想让它更大。我只是不希望他们将其缩小并隐藏其中的一部分。
-
如何在代码中处理该属性,因为该属性仅适用于创建?比如保存窗口大小并制作一个调整大小的监听器,它不允许调整大小小于保存的尺寸?
-
-
设置最小高度和宽度。
标签: java javafx fxml scenebuilder