java 全屏时 Javafx 调整组件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33095166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javafx resize components when fullscreen
提问by Diluk Angelo
Original size screenshot
原始尺寸截图
Fullscreen screenshot
全屏截图
When i resize how can i nicely arrange the components. im using FXML for the GUI
当我调整大小时,我如何很好地安排组件。我在 GUI 中使用 FXML
FXML CODE
文件编码
<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>
回答by ItachiUchiha
The problem is because you are using AnchorPane as your root pane. Though, you can use AnchorPane for scenarios like this, I personallydo not prefer it because you need to do a lot of things to get it right. There are easier ways to do and that is what I am gonna show you.
问题是因为您使用 AnchorPane 作为根窗格。虽然,您可以将 AnchorPane 用于此类场景,但我个人并不喜欢它,因为您需要做很多事情才能正确使用。有更简单的方法可以做到,这就是我要告诉你的。
From the Javadocs :
从 Javadocs :
AnchorPane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges.
AnchorPane 允许将子节点的边缘锚定到与锚窗格边缘的偏移量。
Solution
解决方案
Use a different layout. May be a GridPaneor VBox. These layouts have child alignmentinstead of anchors, which allow the children to be aligned to a particular position.
使用不同的布局。可能是GridPane或VBox。这些布局具有子对齐而不是锚点,这允许子对齐到特定位置。
For re-sizing your children, you can set the HGrow/ VGrowproperty on them.
要重新调整孩子的大小,您可以为他们设置HGrow/ VGrow属性。
Example
例子
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?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>
Here is a small gif on how it looks :
这是一个关于它的外观的小 gif:
回答by Vishnu Katpure
Please add
请加
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"
to your image ,button , lable etc components speciefic size means when screen resize depend on that it will adjust its like Margin in css.
到您的图像、按钮、标签等组件的特定大小意味着当屏幕调整大小取决于它会像 css 中的 Margin 一样调整它的大小时。
Or
或者
you can add css to align this component as per screen size like
您可以添加 css 以根据屏幕大小对齐此组件,例如
Scene scene = new Scene(root);
scene.getStylesheets().add(locator.locateAsURL("Css/StudentApp.css").toExternalForm());
scene.getStylesheets().add(("CSS/studentapp.css"));