2016-09-27 32 views
-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> 

我真的受益從一個簡單的例子。我怎樣才能讓他們保持獨立的文件,並在他們各自保留自己的控制器時合併它們?

+0

爲什麼你想要每個項目都有自己的控制器? –

+0

@MichaelPickett再次檢查xml(編輯)。這些是3個重要的功能,我想用他們自己的控制器和視圖來隔離它們。我只是想打破一切。 – Asperger

+0

您可能可以創建根節點,加載每個FXML文件,並將它們添加到根目錄。然後顯示根階段。這可能工作。 –

回答

0

你可以follow this tutorial

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); 
    } 
} 

在這個例子中,您有兩個FXML文件,RootLayout.fxml和PersonOverview.fxml。 您將您的primarystage場景設置爲(BorderPane)RootLayout.fxml,然後將PersonOverview.fxml添加到BorderPane。