如何在borderpane中将fxml文件加载为完整大小

时间:2019-06-01 14:59:25

标签: javafx

我的目标是获取用户监视器的大小,并相应地显示FXML文件。 但是,该窗口似乎适合该大小,但FXML文件不适合该大小。 如何将文件放入BorderPane中?

此外,希望更改屏幕尺寸时可以自动调整其大小。

我通过参考以下用于窗口控制的YouTube编写了代码。 https://www.youtube.com/watch?v=UzlXUlTD1Lo

Main.java

public class Main extends Application {
FXMLLoader loader = new FXMLLoader();
StackPane pane = new StackPane();
BorderPane mainPane = new BorderPane();
String main="test.fxml";
Scene scene = new Scene(pane, Screen.getPrimary().getVisualBounds().getWidth(), Screen.getPrimary().getVisualBounds().getHeight());

@Override
public void start(Stage primaryStage) {
    try {
        primaryStage.setX(Screen.getPrimary().getVisualBounds().getMinX());
        primaryStage.setY(Screen.getPrimary().getVisualBounds().getMinY());
        mainPane.prefWidthProperty().bind(pane.widthProperty());
        mainPane.prefHeightProperty().bind(pane.heightProperty());
        mainPane = (BorderPane) loader.load(getClass().getResourceAsStream(main));

        pane.getChildren().add(mainPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    } 
    catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}

}

test.fxml

    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="922.0" prefWidth="1314.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar prefHeight="18.0" prefWidth="1078.0" style="-fx-background-color: #f0efef;">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <ToolBar prefHeight="43.0" prefWidth="1078.0" style="-fx-background-color: #b0aacd;">
               <items>
                  <Label prefHeight="26.0" prefWidth="72.0" text="&gt;&gt;&gt;">
                     <font>
                        <Font size="18.0" />
                     </font>
                  </Label>
                  <TextField fx:id="ip" prefHeight="29.0" prefWidth="198.0" />
                  <Button fx:id="start" mnemonicParsing="false" onAction="#onIp" prefHeight="29.0" prefWidth="88.0" style="-fx-background-color: #ddeedd;" text="start" />
               </items>
            </ToolBar>
         </children>
      </VBox>
   </top>
   <center>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <AnchorPane prefHeight="221.0" prefWidth="1078.0">
               <children>
                  <Label layoutX="14.0" layoutY="11.0" prefHeight="34.0" prefWidth="210.0" text="Chapter1">
                     <font>
                        <Font name="Expo M" size="25.0" />
                     </font>
                  </Label>
                  <Separator layoutX="9.0" layoutY="46.0" prefHeight="3.0" prefWidth="1221.0" />
                  <Label layoutX="620.0" layoutY="57.0" text="age" AnchorPane.leftAnchor="620.0">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <Label layoutX="291.0" layoutY="57.0" text="name" AnchorPane.leftAnchor="291.0">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <Label layoutX="301.0" layoutY="99.0" text="choice" AnchorPane.leftAnchor="301.0">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="age" layoutX="677.0" layoutY="57.0" prefHeight="29.0" prefWidth="126.0" />
                  <ChoiceBox layoutX="416.0" layoutY="99.0" prefHeight="26.0" prefWidth="51.0" />
                  <Button fx:id="submit" layoutX="1120.0" layoutY="141.0" mnemonicParsing="false" style="-fx-background-color: #b0aacd;" text="submit">
                     <font>
                        <Font name="Ebrima Bold" size="15.0" />
                     </font>
                  </Button>
                  <TextField fx:id="name" layoutX="341.0" layoutY="57.0" prefHeight="29.0" prefWidth="126.0" />
                  <CheckBox fx:id="male" layoutX="615.0" layoutY="99.0" mnemonicParsing="false" text="male">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </CheckBox>
                  <CheckBox fx:id="female" layoutX="712.0" layoutY="99.0" mnemonicParsing="false" text="female">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </CheckBox>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="227.0" prefWidth="1078.0">
               <children>
                  <Label layoutX="14.0" layoutY="16.0" prefHeight="32.0" prefWidth="179.0" text="Chapter2">
                     <font>
                        <Font name="Expo M" size="25.0" />
                     </font>
                  </Label>
                  <Separator layoutX="14.0" layoutY="48.0" prefHeight="7.0" prefWidth="1225.0" />
                  <Label layoutX="176.0" layoutY="67.0" text="aaa">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text1" editable="false" layoutX="285.0" layoutY="66.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="450.0" layoutY="67.0" text="ddd">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text2" editable="false" layoutX="598.0" layoutY="66.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="767.0" layoutY="67.0" text="ggg">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text3" editable="false" layoutX="900.0" layoutY="66.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="746.0" layoutY="155.0" text="Result">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text8" editable="false" layoutX="900.0" layoutY="155.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="174.0" layoutY="110.0" text="bbb">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text4" editable="false" layoutX="285.0" layoutY="110.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="458.0" layoutY="110.0" text="eee">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text5" editable="false" layoutX="598.0" layoutY="110.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="178.0" layoutY="155.0" text="ccc">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text6" editable="false" layoutX="285.0" layoutY="155.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="458.0" layoutY="155.0" text="fff">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="texxt7" editable="false" layoutX="598.0" layoutY="155.0" prefHeight="26.0" prefWidth="110.0" />
                  <Button fx:id="submit1" layoutX="1127.0" layoutY="170.0" mnemonicParsing="false" style="-fx-background-color: #b0aacd;" text="submit">
                     <font>
                        <Font name="Ebrima Bold" size="15.0" />
                     </font>
                  </Button>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="444.0" prefWidth="1078.0">
               <children>
                  <Label layoutX="491.0" layoutY="9.0" text="List" textAlignment="CENTER">
                     <font>
                        <Font name="Expo M" size="25.0" />
                     </font>
                  </Label>
                  <Separator layoutX="7.0" layoutY="40.0" prefHeight="14.0" prefWidth="1225.0" />
                  <TableView fx:id="List" layoutX="11.0" layoutY="56.0" maxHeight="-Infinity" prefHeight="380.0" prefWidth="1288.0">
                    <columns>
                      <TableColumn fx:id="No" prefWidth="34.0" style="-fx-background-color: white;" />
                        <TableColumn fx:id="a" prefWidth="83.0" style="-fx-background-color: white;" text="a" />
                        <TableColumn fx:id="b" prefWidth="256.0" style="-fx-background-color: white;" text="b" />
                        <TableColumn fx:id="c" prefWidth="90.0" style="-fx-background-color: white;" text="c" />
                        <TableColumn fx:id="d" prefWidth="89.0" style="-fx-background-color: white;" text="d" />
                        <TableColumn fx:id="e" prefWidth="135.0" style="-fx-background-color: white;" text="e" />
                        <TableColumn fx:id="f" prefWidth="87.0" style="-fx-background-color: white;" text="f" />
                        <TableColumn fx:id="g" prefWidth="75.0" text="g" />
                        <TableColumn fx:id="h" prefWidth="75.0" style="-fx-background-color: white;" text="h" />
                        <TableColumn fx:id="i" prefWidth="149.0" style="-fx-background-color: white;" text="i" />
                        <TableColumn fx:id="j" prefWidth="135.0" style="-fx-background-color: white;" text="j" />
                        <TableColumn fx:id="k" prefWidth="75.0" style="-fx-background-color: white;" text="k" />
                    </columns>
                  </TableView>
               </children>
            </AnchorPane>
         </children>
      </VBox>
   </center>
</BorderPane>

FXML文件位于中间,而不是屏幕尺寸。

1 个答案:

答案 0 :(得分:1)

您的代码有两点要指出:

  1. 您要创建一个新的BorderPane,然后在加载FXML时将其覆盖。无需致电BorderPane mainPane = new BorderPane()
  2. 在加载FXML的BorderPane之后,出于某种原因将其放入StackPane中,但切勿在希望其子元素布局方面配置StackPane
  3. 没有必要像这样确定屏幕分辨率/尺寸。您可以致电primaryStage.setMaximized(true)primaryStage.setFullscreen(true),具体取决于您是否希望看到舞台边框。

考虑以下我已简化的代码段:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            // Create the loader and pass our text.fxml reference to it
            FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml"));

            // If you do not specify a controller in the FXML itself, you can do so here
            loader.setController(new Controller());

            // Set our Stage's scene and load the FXML at the same time
            primaryStage.setScene(new Scene(loader.load()));

            // Maximize the stage (or set to fullscreen
            primaryStage.setMaximized(true);
//            primaryStage.setFullScreen(true);

            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  

结果:

screenshot