-1
我找到的最好的文章是:How to create multiple javafx controllers with different fxml files?合併多個FXML,並有一個控制器爲每個文件
但是我真的感到困惑的是如何工作的。所有的例子對於最初的學習來說似乎都有些複雜。
所以這裏我有一個簡單的helloWorld用於測試目的。正如你在xml中看到的,我有一個容器,菜單和頁腳。不過,我想他們的所有3個有獨立的控制器和XML文件,然後將它們合併,顯示爲後級以下的XML看出:
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
<?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>
我真的受益從一個簡單的例子。我怎樣才能讓他們保持獨立的文件,並在他們各自保留自己的控制器時合併它們?
爲什麼你想要每個項目都有自己的控制器? –
@MichaelPickett再次檢查xml(編輯)。這些是3個重要的功能,我想用他們自己的控制器和視圖來隔離它們。我只是想打破一切。 – Asperger
您可能可以創建根節點,加載每個FXML文件,並將它們添加到根目錄。然後顯示根階段。這可能工作。 –