我有这个非常简单的表单,我将所有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中删除填充和间隙,但奇怪的行为仍然存在。
答案 0 :(得分:6)
在显示舞台后,调用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);
}
}
我没有在所有平台上测试过这个。
答案 1 :(得分:0)
我基于James_D's answer编写了这两个函数。在我的应用程序中,我在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而不是Node.getBoundsInLocal。
答案 2 :(得分:-3)
你应该通过写:
来阻止窗口调整大小primaryStage.setResizable(false);