问题
I've got a problem with the view of the application gui in Scene Builder in comparison to running one.
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:
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");
}
}
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.
回答1:
In SceneBuilder, set the Max Height and Max Width to USE_PREF_SIZE
. Likewise, you can also set the min values.
Also, for a scene such as this, BorderPane might be overkill. You could easily use a StackPane with an VBox, or even a GridPane.
!! Edit !!
Try this:
<?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>
来源:https://stackoverflow.com/questions/33709825/why-does-the-running-javafx-application-scene-size-differ-from-the-scenebuilder