9

JavaFX之多个FXML加载和通信

 4 years ago
source link: http://www.cnblogs.com/fordes/p/12491704.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前言

在使用了FXML设计布局后,新的问题随之而来,当一个程序需要多个界面时,我们不可能在一个FXML中写出全部布局,这样太过于臃肿不易查看和维护(当然非要这么做也是可以的),这里就涉及到如何 在一个FXML中添加另一个FXML 以及 多个FXML对应Controller类之间的通讯 。经过查阅发现方法还不止一个,这里分别例举一下吧。

本文目录

方式1

这个方法也是我一开始使用的,原理上就是利用 FXMLLoader 加载FXML然后通过代码添加,假设现有JavaFx项目结构如下:

project
│
└───java
│   │   
│   └───main.java
│   │
│   └───controller
│       ├──mainController.java
│       ├──partController.java
│   
└───resources
    ├── main.FXML
    ├──  part.FXML

首先以正常情况加载 main.FXML ,那么对于part.FXML,可以在 mainControllerinitializable 方法中使用 FXMLLoader 方法从FXML中获取布局,再以代码方式添加到main布局中,有如下伪代码:

/**
* mainController.java
* 伪代码仅用作示例,无法运行
*/
public class mainController implements Initializable {

    @FXML
    private GridPane root;
    private GridPane part;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        try {
            part=new FXMLLoader(getClass().getClassLoader().getResource("resources/part.fxml")).load();
            root.add(part);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

那么Controller之间的通信,同样以上方项目结构为例,假定需要在partController中和mainController实现通信,可以利用 HashMap 作为Controller容器来实现传递。我们有如下代码:

mian.java

public class main extends Application {

    //创建一个Controller容器
    public static Map<String, Object> controllers = new HashMap<String, Object>();
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = new FXMLLoader(getClass().getClassLoader().getResource("resources/main.fxml")).load();
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

如此,在 partController.java 定义如下属性:

private mainControl controller= (mainControl) main.controllers.get(mainControl.class.getSimpleName());

那么在 partController 中,我们就可以使用 controller.xxx 调用 partController 中的公有属性和方法了。

此例与上例没有直接关系

方式2

在使用过程中,我们可以发现,方法1虽然可以实现通信,但是并不好用,这里就要介绍方法2了。同样的,还是先从FXML中添加FXML说起。查了资料发现通过 属性可以直接实现...

同样的以上面项目结构为例,在 main.fxml 中添加 part.fxml 有如下伪代码:

mian.fxml

<AnchorPane fx:id="root" fx:controller="java.controller.mainController" prefHeight="400.0" prefWidth="600.0">
    <children>
        <fx:include fx:id="part" source="TitlesBar.fxml"/>
    </children>
</AnchorPane>

这样就行了,接下来接着看通信,注意这里前后是连着的,不像上一个方法,前后没有直接关系。

mainController.java 中,设置如下属性:

@FXML
    private partController controller;

那么在 mainController 中,我们就可以使用 controller.xxx 调用 partController 中的公有属性和方法了。

完事,结论很明确,方式2更好

参考文章:[https://blog.csdn.net/qq_20336817/article/details/80055701)

(本文最后更新于2020.3.14,原创文章,转载请注明)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK