【问题标题】:Merge multiple FXML and have one controller for each file合并多个 FXML 并为每个文件设置一个控制器
【发布时间】:2017-02-05 12:01:48
【问题描述】:

我找到的最好的文章是:How to create multiple javafx controllers with different fxml files?

但是我真的很困惑这是如何工作的。对于最初的学习,所有示例似乎都太复杂了。

所以在这里我有一个简单的 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 个重要的功能,我想将它们与它们自己的控制器和视图隔离开来。我只想打破一切。
  • 您可以创建一个根节点,加载每个 FXML 文件,并将它们添加到根节点。然后显示根阶段。这可能有效。
  • 您查看的链接正是您想要做的。

标签: java javafx fxml


【解决方案1】:

你可以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。 您将主舞台的场景设置为 (BorderPane)RootLayout.fxml,然后将 PersonOverview.fxml 添加到 BorderPane。

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 2016-10-24
    • 2015-06-03
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多