Java 合并多个FXML,每个文件有一个控制器
我找到的最好的文章是: 然而,我真的很困惑这是怎么回事。对于最初的学习来说,所有的例子似乎都有点太复杂了 这里我有一个简单的helloWorld用于测试。正如您在xml中看到的,我有一个容器、菜单和页脚。但是,我希望这三个类都有单独的控制器和XML文件,然后合并这些控制器和XML文件,并在类后如下图所示:Java 合并多个FXML,每个文件有一个控制器,java,javafx,fxml,Java,Javafx,Fxml,我找到的最好的文章是: 然而,我真的很困惑这是怎么回事。对于最初的学习来说,所有的例子似乎都有点太复杂了 这里我有一个简单的helloWorld用于测试。正如您在xml中看到的,我有一个容器、菜单和页脚。但是,我希望这三个类都有单独的控制器和XML文件,然后合并这些控制器和XML文件,并在类后如下图所示: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.Even
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.setLocation(getClass().getResource("main.fxml"));
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
MainController mainController = loader.getController();
}
}
XML
我可以从一个简单的例子中获益。如何将它们保存在单独的文件中并合并,而它们各自保留自己的控制器?您可以
在本例中,您有两个fxml文件,RootLayout.fxml和PersonOverview.fxml。
将primarystage的场景设置为(BorderPane)RootLayout.fxml,然后将PersonOverview.fxml添加到BorderPane 为什么希望每个项目都有自己的控制器?@MichaelPickett再次检查xml(已编辑)。这是三个重要的功能,我想用它们自己的控制器和视图来隔离它们。我只是想把一切都分解一下。你可以创建一个根节点,加载每个FXML文件,然后将它们添加到根节点。然后显示根阶段。这可能会起作用。您查看的链接的可能副本正是您想要做的。
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.canvas.*?>
<HBox fx:id="container" id="container" fx:controller="core.GuiController" xmlns:fx="http://javafx.com/fxml">
<HBox fx:id="post" id="post">
<!-- Stuff -->
</HBox>
<HBox fx:id="friends" id="friends">
<!-- Stuff -->
</HBox>
<HBox fx:id="profile" id="profile">
<!-- Stuff -->
</HBox>
</HBox>
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
initRootLayout();
showPersonOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}