在一个开发多窗口桌面应用程序的项目上,我正在学习JavaFX + FXML。稍后,针对这些窗口之间的一些依赖关系( gui 1中的更改会刷新gui 2、gui x. )。
现在我在想,经验丰富的javafx开发人员是如何构造他们的项目的?
他们如何使用多窗口?
如何打开和重新初始化已经打开的窗口?
他们如何在所有类中处理这个问题?
如何处理数据库连接/sql调用?
我正在尝试使用某种MVC模式进行开发,为每个窗口使用一个包:
ModelWindowname.java -- Model
Windowname.fxml -- View
Windowname.css --
ControllerWindowname.java -- Controller然后,我很难找到一种方法来初始化来自不同类的窗口。我可以通过创建自定义加载器对象和创建我自己的初始化方法来解决这个问题(下面的例子)。
@FXML
void openGuiOne(ActionEvent event) throws IOException {
/*
* We don't put the FXMLLoader into try/catch blocks, so we throw the IOException.
* Why? The user has no impact on what's going on here, so we can avoid the
* overload on code and trace-information.
*/
Stage oStage = null;
Parent oScene = null;
// Check if the stage is already opened
if (StageHandler.stageAlreadyOpened("gui_one") == false) {
// if not, open a new window based on the One.fxml design and set some properties
oScene = FXMLLoader.load(getClass().getResource("/com/mytool/one/One.fxml")); // requires to throw IOException
oStage = new Stage();
oStage.setScene(new Scene(oScene));
oStage.setTitle("One");
oStage.setResizable(false);
oStage.setX(475.0); // Set x-position of the window on startup
oStage.setY(410.0); // Set y-position of the window on startup
oStage.show();
// Look into StageHandler-Class for detailed information about the class
StageHandler.addStage("gui_one", oStage);
// EventHandler - close only this window if it's closed
oStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
StageHandler.delStage("gui_one");
}
});
} else {
// if window is already opened, pull it to the foreground
StageHandler.getStage("gui_one").toFront();
}
}如果我想从另一个gui上的一个按钮打开gui_one,那么主gui是如何实现的呢?现在我就是这么做的:
@FXML
void openGuiOne(ActionEvent event) throws IOException, SQLException {
/*
* We don't put the FXMLLoader into try/catch blocks, so we throw the IOException.
* Why? The user has no impact on what's going on here, so we can avoid the
* overload on code and trace-information.
*/
Stage oStage = null;
FXMLLoader oLoaderOne = new FXMLLoader(getClass().getResource("/com/mytool/one/One.fxml"));
// Check if the stage is already opened
if (StageHandler.stageAlreadyOpened("gui_one") == false) {
// if not, open a new window based on the One.fxml design and set some properties
oStage = new Stage();
oStage.setScene(new Scene(oLoaderOne.load()));
ControllerOne oControllerOne = oLoaderOne.getController();
oStage.setTitle("One");
oStage.setResizable(false);
oStage.setX(475.0); // Set x-position of the window on startup
oStage.setY(50.0); // Set y-position of the window on startup
oControllerOne.myInitialize("");
oStage.show();
// Look into StageHandler-Class for detailed information about the class
StageHandler.addStage("gui_one", oStage);
// EventHandler - close only this window if it's closed
oStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
StageHandler.delStage("gui_one");
}
});
} else {
// if window is already opened, pull it to the foreground
StageHandler.getStage("gui_one").toFront();
}
}但是,对于我能够打开的每个gui,我总是要在每个控制器上复制大量的代码行吗?嗯,由于我必须在多个地方改变它,我甚至不会去想它。但现在我不知道怎么处理这件事。我相信我对oop没什么意见,我从来没有用oop创建过桌面应用程序。
到目前为止,我对研究这些问题的印象是:几乎每一秒的回答都是“这是唯一的出路”,但每个答案都是不同的。
我找到了这篇文章practices.htm],但不幸的是,我找不到任何其他(值得信任的)东西。在javafx上有任何小型/中型/大型专业开源项目吗?我的第一次尝试是了解开源项目以及大型项目的结构.我还找不到桌面应用程序。或者有人知道一个我完全错过的最好的实践指南,它正在被更多的人所遵循,而不仅仅是几个人?
诚挚的问候。
发布于 2017-04-19 07:24:17
你应该做的第一件事就是不要像这里这么多人那样重新发明轮子。看看JavaFX的现有应用程序框架之一,例如MVVMFX https://github.com/sialcasa/mvvmFX/wiki,这样的应用程序框架将为您的项目提供一个坚实的结构,您可以在此基础上进行构建。
https://stackoverflow.com/questions/43488789
复制相似问题