Javafx 如何在应用程序启动时在BorderPane中心加载FXML文件

Javafx 如何在应用程序启动时在BorderPane中心加载FXML文件,javafx,Javafx,如何加载FXML文件并使其在启动时显示在主视图的中心 @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("Main_Scene.fxml")); primaryStage.setTitle("MY APP"); BorderPane mainP

如何加载FXML文件并使其在启动时显示在主视图的中心

 @Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("Main_Scene.fxml"));
    primaryStage.setTitle("MY APP");

    BorderPane mainPane = FXMLLoader.load(getClass().getResource("Options.fxml"));
    mainPane.setCenter();
    primaryStage.setScene(new Scene(root, 1024 , 768));
    primaryStage.show();
}


public static void main(String[] args) { launch(args); }
更新:我已经这样做了,它似乎工作

 Pane view = FXMLLoader.load(getClass().getResource("SelectPayment.fxml"));
    Pane view2 = FXMLLoader.load(getClass().getResource("Options.fxml"));
    mainPane = new BorderPane();
    mainPane.setLeft(view);
    mainPane.setCenter(view2);

    primaryStage.setScene(new Scene(mainPane, 1024 , 768));
    primaryStage.show();
我就是这样做的:

  • Main_Scene.fxml中
    AnchorPane
    包装在
    StackPane中
  • 将第二个
    StackPane
    添加到顶层
    StakPane
    子级
  • 在里面放一个居中的HBox
  • 在里面放一个中心
    VBox
  • 现在,您可以将控件添加到
    VBox
    ,它将显示在中间

    <StackPane fx:id="root" prefHeight="1024" prefWidth="1400" stylesheets="@../styles/style.css" xmlns="http://javafx.com/javafx/8.0.202" xmlns:fx="http://javafx.com/fxml/1" fx:controller="path.to.MainController">
        <children>
            <AnchorPane id="AnchorPane" fx:id="rootMain" prefHeight="800.0" prefWidth="1284.0">
            ...
            <AnchorPane>
            <StackPane>
                <children>
                    <HBox alignment="CENTER">
                        <children>
                            <VBox alignment="CENTER" fx:id="centreBox" />
                        </children>
                    </HBox>
                </children>
            </StackPane>
        </children>
    </StackPane>
    
    
    ...
    
    FXML文件通常会定义边框窗格及其内容(包括中间的内容)。在这种情况下,您不需要调用
    mainPane.setCenter()。如果您真的想执行您描述的操作,您可以在Java代码中创建一个
    BorderPane
    ,并在其上调用
    setCenter(…)
    ,传递从FXML文件加载的任何内容。
    Pane view = FXMLLoader.load(getClass().getResource("SelectPayment.fxml"));
    Pane view2 = FXMLLoader.load(getClass().getResource("Options.fxml"));
    mainPane = new BorderPane();
    mainPane.setLeft(view);
    mainPane.setCenter(view2);
    
    primaryStage.setScene(new Scene(mainPane, 1024 , 768));
    primaryStage.show();