JavaFX项目结构 [英] JavaFX Project Structure

查看:264
本文介绍了JavaFX项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用FXML的JavaFX的MVC模型听起来很棒,除非我有麻烦找到如何组织我的项目包。



每个教程我找到关于JavaFX是太简单和无组织:他们只是创建一个包,创建一切,每个控制器,每个fxml,每个css。我不想要。我想要的东西在正确的地方。



但是,JavaFX的路径似乎...limitive。使用URL使得如果我想限制我的资源到本地文件,我必须做整个 getClass()。getResource(foo.fxml)。openStream()事情。这是伟大的,但通过从类路径获取资源,路径是从该类所在的包。我有点想项目的根。这将简化我的生活,但是JavaFX似乎并不像这样工作。



让我们得到一个实际的例子:



<想象一下我有一个FXML登录屏幕。想象一下,我想要的登录屏幕使用样式表。理想情况下,css将在该fxml的同一个包中。但是如果我想在另一个FXML中使用相同的.css怎么办?这是否意味着我必须将两个FXML放在同一个包?显然我不需要,但是我该怎么办呢?



此外,让我说当我正确登录时,我想改变场景。在FXML控制器正确的事件中,我将调用一个setScene。这个路径也很难获得,因为如果我有FXML在不同的包。看来,无论是一切都在一个巨大的膨胀的包或一切都很难访问,而不采取像../../ dir的黑客。



亨利 http://docs.oracle.com/javafx/2/best_practices/jfxpub-best_practices中的销售应用程序.htm 似乎是一个组织良好的应用程序的例子,虽然应用程序是一个单一的TabPane。不幸的是(至少我认为)源不是开放的。它的想法是这样的:


 客户端
Main.class
styles.css
client.images
image.png
client.screen1
Screen1.fxml
Screen1Controller.java
client.screen2
Screen2 .fxml
Screen2Controller.java
...


这个doenst似乎是一个糟糕的开始,但它有一些问题(或至少我看到他们的问题)。



对于亨利销售它聪明地有一个Main,它会调用一个包的FXML(容易访问,FXML的目录下面Main类)。仍然,对于样式表,这将必须硬编码 scene.getStylesheets()。add(...); 。我真的更愿意选择在FXML中选择我的样式表。毕竟,样式表是View组件的一部分。从FXML中的URL访问.css文件对于这种结构很难,因为它在它们的目录之上。



此外,对于这个组织,场景竞争?在这个项目中,这是不需要的,因为整个项目是一个单一的TabbedPane。主调用它,它做了。不需要更多的互换。但是一个简单的登录场景(或者为什么需要交换整个场景的原因)需要访问FXML路径。



然后是资源。 Css文件可能需要使用图像。该结构通过将.css文件放在顶部,并为.css可能需要的文件创建一个包来解决它。如果我想要一个特定的FXML有不同的.css,那么另一个问题会到达。



看起来像一个循环。 Css需要访问共享资源文件夹。 FXML需要访问Css。 FXML的控制器需要访问其他FXML。我希望我清楚我的项目结构的怀疑。请帮助我创建一个JavaFX项目结构,这对于一个超过基本的应用程序是足够强大的,或者将我重定向到一些好的源代码。



解决方案

IMHO,您不应该根据您的屏幕创建软件包!



我对这种应用程序的方法




  • 对应控制器的软件包 c>

  • 如果存在服务(业务)和dao(持久)层,则不同的包

  • 用于资源(例如图片,css等)的包/文件夹

  • fxml的名为视图的包 in the resources

      src / main / java 
    |
    controllers
    |
    Screen1controller.java
    Screen2controller.java
    service
    |
    Service1.java
    dao(persist)
    |
    SaveProducts.java
    src / main / resources
    |
    view
    |
    screen1.fxml
    screen2.fxml
    css
    |
    style.css
    |
    图片
    |
    img1.jpg
    img2.jpg




上面的实现可以考虑用于 Maven 项目。



对于一个简单的项目,可以在此处查看结构。这是一个maven项目!


JavaFX's MVC Model by using FXML sounds awesome and all but I'm having trouble find out how to organize my project packages.

Every single tutorial i find about JavaFX is way too simple and unorganized: They simply create ONE package and create everything there, every controller, every fxml, every css. I dont want that. I want things to be in their right places.

Still, JavaFX's "pathing" seems... "limitive". The use of URLs make it so that if I want to limit my resources to local files, I have to do the whole getClass().getResource("foo.fxml").openStream() thing. That is great, but by getting resources from a class path, the path is from the package that class is in. I kinda wanted the project's root. That would simplify my life, but JavaFX doesnt seems to work like that.

Lets get to a practical example:

Imagine I have an FXML "Login Screen". Imagine I want that Login Screen to use a stylesheet. Ideally, that css would be in the same package of that fxml. But what if I want to use the same .css in another FXML? Would that mean I have to place both FXML in the same package? Obviously I "dont need to", but how do I do it then?

Also, lets say i want to change scene when I login properly. In the FXML Controller proper event, I would have to call a "setScene". That path would also be difficult to obtain since if I have the FXML's in different packages. It just seems that either everything is in one giant bloated package or everything is hard to access without resorting to hacks like "../../dir".

The Henley Sales application in http://docs.oracle.com/javafx/2/best_practices/jfxpub-best_practices.htm seems to be an example of a well organized application, although the application is a single TabPane. Unfortunatly (at least I think) the source is not open. Its idea is something like this:

client
  Main.class
  styles.css
      client.images
          image.png
      client.screen1
          Screen1.fxml
          Screen1Controller.java
      client.screen2
          Screen2.fxml
          Screen2Controller.java
      ...

This doenst seem like a bad start, but it has a few problems (or at least I see them as problems).

For 'The Henley Sales', Its smart to have a Main which would call one of the packages' FXML (easy access, FXML's directories are below Main class). Still, for the stylesheet, that would have to be hardcoded by scene.getStylesheets().add(...);. I really would prefer to have the choice of choosing my stylesheet in the FXML. Afterall, stylesheet is part of the View component. Accessing the .css file from an URL in the FXML's would be kinda hard with this structure, since its above their directories.

Also, with this organization, how would I change scene competely? In this project, that isnt needed because the whole project is a single TabbedPane. Main calls it, and its done. No need for more swaps. But a simple Log in scene(or whatever is the reason why one would need to swap the entire scene) calls for a need to access FXML paths.

And then there's the resources. Css files might need to use images. That structure solves it by placing the .css file on top, and creating a package just for the files the .css might need. If I wanted a particular FXML to have a different .css, tho, another problem would arrive.

It seems like a cycle. Css needs access to a shared resource folder. FXML needs access to Css. FXML's controllers need access to other FXML's. I hope I was clear about my project structure doubts. Please help me on creating a JavaFX project structure which is powerful enough for a more-than-basic application, or redirect me to some good source code.

Oh, Im using Netbeans by the way.

解决方案

IMHO, you shouldn't create package's depending on your screen's !

My approach towards such applications

  • A package for corresponding controllers of these views
  • Different package's for service(business) and dao(persistence) layer, if exists
  • A package/folder for resources such as images, css etc
  • A package for fxml's called view in the resources

    src/main/java
      |
      controllers
         |
         Screen1controller.java
         Screen2controller.java
      service
         |
         Service1.java
      dao(persist)
         |
         SaveProducts.java
    src/main/resources
      |
      view
         |
         screen1.fxml
         screen2.fxml
      css
         | 
         style.css
      |
      images
         |
         img1.jpg
         img2.jpg
    

The above implementation can be considered for a Maven project.

For a simple project, you can view a structure here. It is a maven project!

这篇关于JavaFX项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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