适用于小型/中型/大型桌面应用程序的JavaFX最佳实践指南?

时间:2017-04-19 07:07:29

标签: java javafx

我正在开发一个开发多窗口桌面应用程序的项目中学习 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创建一个桌面应用程序。

到目前为止,我对研究这些问题的印象是:几乎每一秒的答案都是&#34;这是唯一的方法。但每个答案都不一样。

我发现了这篇文章[http://docs.oracle.com/javafx/2/best_practices/jfxpub-best_practices.htm],但遗憾的是我无法找到任何信任的信息。 在javafx上有任何小型/中型/大型专业开源项目吗?我的第一次尝试是了解开源项目以及大型项目的结构......我还无法找到桌面应用程序。 或者有没有人知道我完全错过的最佳实践指南,哪一个会被更多的人跟进呢?

最好的问候。

1 个答案:

答案 0 :(得分:2)

你应该做的第一件事就是不要像这么多人那样重新发明轮子。查看JavaFX的现有应用程序框架之一,例如: MVVMFX https://github.com/sialcasa/mvvmFX/wiki 这样的应用程序框架将为您的项目提供可靠构建的可靠结构。