Javafx 如何在应用程序启动时在BorderPane中心加载FXML文件
如何加载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
@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
子级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();