JavaFX - 许多静态FXML控制器 [英] JavaFX - Many Static FXML Controllers

查看:148
本文介绍了JavaFX - 许多静态FXML控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在 JavaFX 应用程序,应用程序从 Main.class 文件开始,该文件 extends Application

  public class Main extends Application {

/ **
*保留对主舞台
* /
公共静态舞台舞台的参考;
/ **
* MainScene Controller
* /
public static MainScene mainSceneController;
/ **
*应用程序的捕获窗口
* /
public static CaptureWindow captureWindowController;
/ **
*设置场景控制器
* /
public static SettingsController settingsController;

@Override
public void start(Stage primary)抛出异常{

stage = primary;
..........

// CaptureWindow
FXMLLoader loader1 = new FXMLLoader(getClass()。getResource(/ fxml / CaptureWindow.fxml) );
loader1.load();
captureWindowController = loader1.getController();

// MainScene
mainSceneController = new MainScene();

.........
}

}






描述



如上所示我可以看到有3 FXMLControllers (一个是可重用的[扩展StackPane ],其他没有)。我已声明所有这些 static 因为我想从其他 FXMLControllers 中的一个 FXMLController 访问变量。我使用这个策略每次我使用 JavaFX 而且我觉得不好......



我如何更改代码下面所以我可以从其他 FXMLController 访问一个 FXMLController 的变量或方法?这可能不使用 static 关键字吗?



考虑控制器是由不同的类表示的,它们可以是不同的套餐。






在写这个问题之前,我看了一眼





欲了解更多信息,您可以阅读( http://www.wildcrest.com/Potel/Portfolio/mvp.pdf






有关JavaFX的示例,请查看 James_D 在此处回答(使用JavaFx应用MVC






最后但并非最不重要的是看看评论这里



最后:



如果有任何不准确之处,请随时编辑。


A JavaFX application exists and the application is starting from the Main.class file which extends Application:

  public class Main extends Application {

    /**
     * Keep a reference to the main Stage
     */
    public static Stage                 stage;
    /**
     * MainScene Controller
     */
    public static MainScene             mainSceneController;
    /**
     * The Capture Window of the application
     */
    public static CaptureWindow         captureWindowController;
    /**
     * Settings Scene Controller
     */
    public static SettingsController    settingsController;

    @Override
    public void start(Stage primary) throws Exception {

        stage = primary;
        ..........

        // CaptureWindow
        FXMLLoader loader1 = new FXMLLoader(getClass().getResource("/fxml/CaptureWindow.fxml"));
        loader1.load();
        captureWindowController = loader1.getController();

        // MainScene
        mainSceneController = new MainScene();

        .........   
    }

  }


Description

As you can see above I have 3 FXMLControllers(one is reusable[extends StackPane],others not).I have declared all of them static cause i want to access variables from one FXMLController from the other FXMLControllers.I use this strategy every time I use JavaFX and I don't think is good...

How I can change the code below so I can access variables or methods of one FXMLController from other FXMLController? Is that possible without using static keyword?

Consider that the Controllers are represented from different classes which can be in different packages.


Also before writing this question I had a look Static @FXML variables in FXML Controller

解决方案

Actually the answer for this question seems a little bit complicated it has to do with MVC pattern and it's evolution until now.We will use MVP Pattern.

After a long discussion i got a link on this website http://martinfowler.com/eaaDev/uiArchs.html defining the historical evolution of the different patterns used from the old ages of Smalltalk until now.


The actually solution is using Model Viewer Presenter Pattern(MVP) which can be visually described using these images:

For more you can read(http://www.wildcrest.com/Potel/Portfolio/mvp.pdf)


For an example on JavaFX have a look on James_D answer here (Applying MVC With JavaFx)


Last but not least have a look at the comments here:

Finally:

If anything is inaccurate feel free to edit.

这篇关于JavaFX - 许多静态FXML控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆