[英]Javafx resize components when fullscreen
原始尺寸截圖
全屏截圖
當我調整大小時,我如何很好地安排組件。 我在 GUI 中使用 FXML
FXML 代碼
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="style.LoginController">
<children>
<Button fx:id="login" layoutX="250.0" layoutY="242.0" mnemonicParsing="false" onAction="#login" prefHeight="32.0" prefWidth="101.0" text="Login" />
<ImageView fx:id="img2" fitHeight="32.0" fitWidth="32.0" layoutX="109.0" layoutY="184.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../Downloads/Drawing%20(23).png" />
</image>
</ImageView>
<Label fx:id="title" alignment="CENTER" focusTraversable="false" layoutX="166.0" layoutY="48.0" opacity="0.83" prefHeight="42.0" prefWidth="269.0" styleClass="title" text="Label" textAlignment="CENTER" textOverrun="CENTER_ELLIPSIS">
<font>
<Font name="Arial Narrow" size="36.0" />
</font>
</Label>
<TextField fx:id="txtusername" layoutX="159.0" layoutY="126.0" prefHeight="32.0" prefWidth="283.0" promptText="Username" styleClass="fields" />
<PasswordField fx:id="txtpassword" layoutX="159.0" layoutY="183.0" prefHeight="32.0" prefWidth="283.0" promptText="Password" styleClass="fields" />
<ImageView fx:id="img1" fitHeight="32.0" fitWidth="32.0" layoutX="109.0" layoutY="126.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../Downloads/Drawing%20(22).png" />
</image>
</ImageView>
<ProgressIndicator fx:id="progress" layoutX="197.0" layoutY="120.0" minHeight="0.0" minWidth="0.0" prefHeight="128.0" prefWidth="188.0" progress="0.0" styleClass="progressind" />
</children>
</AnchorPane>
問題是因為您使用 AnchorPane 作為根窗格。 雖然,您可以將 AnchorPane 用於此類場景,但我個人並不喜歡它,因為您需要做很多事情才能正確使用。 有更簡單的方法可以做到,這就是我要告訴你的。
從 Javadocs :
AnchorPane 允許將子節點的邊緣錨定到與錨定窗格邊緣的偏移量。
解決方案
使用不同的布局。 可能是GridPane或VBox 。 這些布局具有子對齊而不是錨點,這允許子對齊到特定位置。
要重新調整孩子的大小,您可以為他們設置HGrow / VGrow屬性。
例子
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" spacing="20.0" style="-fx-background-color: DARKCYAN;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="Easy Doctor" />
<TextField promptText="Username" />
<TextField promptText="Password" />
</children>
<padding>
<Insets bottom="50.0" left="50.0" right="50.0" top="50.0" />
</padding>
</VBox>
這是一個關於它的外觀的小 gif:
請加
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"
到您的圖像、按鈕、標簽等組件的特定大小意味着當屏幕調整大小取決於它會像 css 中的 Margin 一樣調整它的大小時。
或者
您可以添加 css 以根據屏幕大小對齊此組件,例如
Scene scene = new Scene(root);
scene.getStylesheets().add(locator.locateAsURL("Css/StudentApp.css").toExternalForm());
scene.getStylesheets().add(("CSS/studentapp.css"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.