JavaFX - 许多静态 FXML 控制器 [英] JavaFX - Many Static FXML Controllers
问题描述
存在一个 JavaFX
应用程序并且该应用程序从 扩展应用程序
的 Main.class
文件开始:
public class Main extends Application {/*** 保持对主舞台的引用*/公共静态舞台舞台;/*** MainScene 控制器*/公共静态 MainScene mainSceneController;/*** 应用程序的捕获窗口*/公共静态 CaptureWindow captureWindowController;/*** 设置场景控制器*/公共静态 SettingsController settingsController;@覆盖公共无效开始(阶段主要)抛出异常{阶段 = 初级;.....//捕获窗口FXMLLoader loader1 = new FXMLLoader(getClass().getResource("/fxml/CaptureWindow.fxml"));loader1.load();captureWindowController = loader1.getController();//主场景mainSceneController = new MainScene();…………}}
<小时>
说明
正如你在上面看到的,我有 3 个 FXMLControllers
(一个是可重用的 [extends StackPane
],其他的不是).我已经声明了所有的 static 因为我想从另一个 FXMLControllers
访问一个 FXMLController
的变量.我每次使用 JavaFX
时都使用这种策略,但我不这样做觉得不好...
我如何更改下面的代码,以便我可以从另一个 FXMLController
访问一个 FXMLController
的变量或方法?不使用 static
关键字可以吗?
考虑到控制器由不同的类表示,这些类可以位于不同的包中.
<小时>在写这个问题之前我也看了
有关更多信息,您可以阅读(http://www.wildcrest.com/Potel/Portfolio/mvp.pdf)
有关 JavaFX 的示例,请查看此处的 James_D
答案(ApplyingMVC 与 JavaFx)
最后但并非最不重要的是看看评论这里:
最后:
如有任何不准确之处,请随时编辑.
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屋!