【发布时间】:2017-04-19 07:07:28
【问题描述】:
我正在一个开发多窗口桌面应用程序的项目中学习 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 创建过桌面应用程序。
到目前为止,我对研究这些问题的印象:几乎每个第二个答案都像是“这是唯一的出路”,但每个答案都是不同的。
我找到了这篇文章 [http://docs.oracle.com/javafx/2/best_practices/jfxpub-best_practices.htm],但不幸的是我找不到任何其他内容(值得信赖)。 javafx上有没有小型/中型/大型专业开源项目?我的第一次尝试是了解开源项目以及大型项目的结构......我还没有找到桌面应用程序。 或者有没有人知道我完全错过的最佳实践指南,并且被更多人遵循?
最好的问候。
【问题讨论】: