如何在 javafx 中为每个 fxml 页面设置大小
how to set size for each fxml page in javafx
我正在使用 javafx 在 eclipse 中创建登录表单和注册表...当应用程序 运行 它显示登录和注册表的默认大小.. 两个表单有不同的大小.. 如何更新它?
代码如下
Samplecontroller.java
public void signin() {
AnchorPane pane2;
try {
pane2 = FXMLLoader.load(getClass().getResource("Login.fxml"))
root.getChildren().setAll(pane2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FXML
<AnchorPane prefHeight="435.0" prefWidth="655.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<ImageView fitHeight="435.0" fitWidth="655.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../image/reg1.jpeg" />
</image>
</ImageView>
<Label layoutX="278.0" layoutY="72.0" prefHeight="22.0" prefWidth="212.0" text="Login" textFill="#f8f7f7">
<font>
<Font name="Arial Bold Italic" size="39.0" />
</font>
</Label>
<Label layoutX="38.0" layoutY="229.0" text="Email id" textFill="#19f236">
<font>
<Font name="System Bold" size="28.0" />
</font>
</Label>
<Label layoutX="38.0" layoutY="309.0" text="Login id" textFill="#19f236">
<font>
<Font name="System Bold" size="22.0" />
</font>
</Label>
<TextField layoutX="204.0" layoutY="237.0" prefHeight="25.0" prefWidth="326.0" />
<TextField layoutX="209.0" layoutY="313.0" prefHeight="25.0" prefWidth="326.0" />
<Button layoutX="252.0" layoutY="374.0" mnemonicParsing="false" text="Login" />
<Button layoutX="428.0" layoutY="374.0" mnemonicParsing="false" text="Cancel" />
<Label layoutX="52.0" layoutY="357.0" textFill="#ee1111">
<font>
<Font size="28.0" />
</font>
</Label>
主要 class
public void start(Stage primaryStage) {
try {
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,300,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
与主要 class 中一样,您将场景宽度和高度设置为 300,它将在启动时覆盖 fxml 文件中设置的高度和宽度。
所以我建议你只设置场景的根
Scene scene = new Scene(root);
这将允许从 fxml 文件中获取高度和宽度
我正在使用 javafx 在 eclipse 中创建登录表单和注册表...当应用程序 运行 它显示登录和注册表的默认大小.. 两个表单有不同的大小.. 如何更新它?
代码如下
Samplecontroller.java
public void signin() {
AnchorPane pane2;
try {
pane2 = FXMLLoader.load(getClass().getResource("Login.fxml"))
root.getChildren().setAll(pane2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FXML
<AnchorPane prefHeight="435.0" prefWidth="655.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<ImageView fitHeight="435.0" fitWidth="655.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../image/reg1.jpeg" />
</image>
</ImageView>
<Label layoutX="278.0" layoutY="72.0" prefHeight="22.0" prefWidth="212.0" text="Login" textFill="#f8f7f7">
<font>
<Font name="Arial Bold Italic" size="39.0" />
</font>
</Label>
<Label layoutX="38.0" layoutY="229.0" text="Email id" textFill="#19f236">
<font>
<Font name="System Bold" size="28.0" />
</font>
</Label>
<Label layoutX="38.0" layoutY="309.0" text="Login id" textFill="#19f236">
<font>
<Font name="System Bold" size="22.0" />
</font>
</Label>
<TextField layoutX="204.0" layoutY="237.0" prefHeight="25.0" prefWidth="326.0" />
<TextField layoutX="209.0" layoutY="313.0" prefHeight="25.0" prefWidth="326.0" />
<Button layoutX="252.0" layoutY="374.0" mnemonicParsing="false" text="Login" />
<Button layoutX="428.0" layoutY="374.0" mnemonicParsing="false" text="Cancel" />
<Label layoutX="52.0" layoutY="357.0" textFill="#ee1111">
<font>
<Font size="28.0" />
</font>
</Label>
主要 class
public void start(Stage primaryStage) {
try {
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,300,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
与主要 class 中一样,您将场景宽度和高度设置为 300,它将在启动时覆盖 fxml 文件中设置的高度和宽度。 所以我建议你只设置场景的根
Scene scene = new Scene(root);
这将允许从 fxml 文件中获取高度和宽度