为什么运行的JavaFX应用程序场景大小与场景构建器预览不同 [英] Why does the running JavaFX application scene size differ from the scenebuilder preview
问题描述
与运行一个应用程序gui相比,我在Scene Builder中遇到了应用程序gui的问题。
I've got a problem with the view of the application gui in Scene Builder in comparison to running one.
场景构建器预览中的应用程序UI看起来像这个:
The app UI in the scene builder preview looks like this:
运行应用程序时,它看起来像:
When running the app it looks like that:
所以元素肯定在不同的地方。
So elements are definitely in different places.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="242.0" prefWidth="465.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<bottom>
<Pane prefHeight="242.0" prefWidth="425.0" BorderPane.alignment="CENTER">
<children>
<TextField layoutX="45.0" layoutY="37.0" />
<TextField layoutX="45.0" layoutY="112.0" />
<Button layoutX="337.0" layoutY="178.0" mnemonicParsing="false" text="Button" />
</children>
</Pane>
</bottom>
</BorderPane>
Main.java:
Main.java:
package com.tempapp;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root1 = (BorderPane)FXMLLoader.load(getClass().getResource("LoginWindows.fxml"));
Scene scene = new Scene(root1);
scene.getStylesheets().add(getClass().getResource("LoginWindows.fxml").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
System.out.println("HELLO");
}
}
应用看起来不同的原因是什么?我试图为scene / stage设置正确的高度和宽度值,但它没有修复它。这是由java虚拟机引起的,因为某些分辨率缩放?我的显示是全高清。
What is the reason that app looks different? I tried to set the proper height and width values for scene/stage, but it doesn't fix it. Is this caused by java virtual machine, becouse of some resolution scaling? My display is full hd.
推荐答案
在SceneBuilder中,将最大高度和最大宽度设置为 USE_PREF_SIZE
。同样,您也可以设置最小值。
In SceneBuilder, set the Max Height and Max Width to USE_PREF_SIZE
. Likewise, you can also set the min values.
此外,对于像这样的场景,BorderPane可能过度。您可以轻松地将StackPane与VBox一起使用,甚至可以使用GridPane。
Also, for a scene such as this, BorderPane might be overkill. You could easily use a StackPane with an VBox, or even a GridPane.
!!编辑!!
试试这个:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="180.0" prefWidth="260.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox layoutX="45.0" layoutY="37.0" spacing="12.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextField fx:id="txtUserName" />
<TextField fx:id="txtPassword" />
<HBox>
<children>
<Pane HBox.hgrow="ALWAYS" />
<Button fx:id="btnOk" mnemonicParsing="false" text="Button" />
</children>
</HBox>
</children>
<padding>
<Insets bottom="18.0" left="18.0" right="18.0" top="18.0" />
</padding>
</VBox>
</children>
</AnchorPane>
这篇关于为什么运行的JavaFX应用程序场景大小与场景构建器预览不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!