[英]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>
我的問題是窗口仍然太小:
我該如何防止這種情況發生?
我當然可以設置一個固定的最小寬度和高度,這是微不足道的:
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中刪除填充和間隙,但奇怪的行為仍然存在。
在顯示舞台后,調用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()
在這里是多余的,因為這是這種情況下的默認行為,但是如果你稍后更換舞台的場景可能需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);
}
}
我沒有在所有平台上測試過這個。
基於James_D的回答 ,我寫了這兩個函數。 在我的應用程序中,我在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 。
您應該通過編寫來阻止窗口調整大小:
primaryStage.setResizable(false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.