Spingboot+maven构建一个javafx的项目,开发并打包

Spingboot+maven构建一个javafx的项目,开发并打包

本文为转载 https://segmentfault.com/a/1190000014037443

       1介绍:

       Javafx的springboot的支持库,官方是没有的,开源的有一大堆,本文采用的是springboot-javafx-support地址是:springboot-javafx-support.这个库文档比较全,比较详细,文档地址:SpringBoot and JavaFx8

springboot必须用maven,否则那简直是灾难。最重要的是打包工具。我用的是JavaFX Maven Plugin地址:JavaFX Maven Plugin

       2、maven配置

具体maven配置可以参见项目代码。这里主要写一些注意事项。Maven配置最重要的是<mainClass>com.spartajet.fxboot.demo.MainController</mainClass>这个是打包的时候的 main 类。<vendor>Spartajet</vendor>是组织名称。

       3javafx集成springboot

       3.1创建FXML布局文件

       可以使用SceneBuilder工具创建FXML文件,我还是建议自己写FXML,刚开始不太习惯,慢慢适应就好了。但是强烈建议布局和样式分开,FXML只管布局,CSS只管样式。

<BorderPaneid="mainBorderPane"xmlns="http://javafx.com/javafx/8.0.111"xmlns:fx="http://javafx.com/fxml/1"fx:controller="com.spartajet.fxboot.demo.controller.MainStageController"stylesheets="/css/MainStage.css"></BroderPane>

       3.2MainStageController

       每一个FXML布局文件都对应一个contriller,要在fx:contorller进行设置。

/**

 * @description

 * @create2017-05-20 下午1:55

 * @email spartajet.guo@gmail.com

 */

@FXMLController

publicclassMainStageControllerimplementsInitializable {

    /**

     * Called to initialize a controller afterits root element has been

     * completely processed.

     *

     * @paramlocation  The location used to resolverelative paths for the root object, or

     *                  <tt>null</tt> ifthe location is not known.

     * @paramresources The resources used to localize the root object, or<tt>null</tt> if

     */

    public voidinitialize(URL location, ResourceBundle resources) {  

    }

实现Initializable接口,加上了@FXMLContorller注解

 

       3.3MainStageView

       这个是比较特殊的,在普通的javafx里面没有这个东西,但是按照MVC的角度来说,业务和视图分离,是很有必要的。

@FXMLView(value= "/view/MainStage.fxml")

public classMainStageViewextendsAbstractFxmlView {    }

这里注意,添加的是@FXMLView注解,这个可以注入CSS样式文件以及bundle文件。本文的CSS文件是在FXML文件中引入的,都可以的。

其中还继承了AbstractFxmlView抽象类,方法比较少,比较重要的是getView方法,返回的是Node对象,然后就可以随意使用这个视图了。

       3.3MainController

       具体代码可看项目。

继承自AbstractJavaFxApplicationSupport可以看源码,典型的Aware。然后就可以运行了。

可以看到

这是启动动画,可以自定义启动动画,个人认为,启动动画还是很有必要的,因为springboot启动费时还是比较多的,来个启动动画,逼格满满的。

       3.4启动动画

/**

 * @description

 * @create2017-05-20 下午2:54

 * @email[email protected]

 */

publicclassCustomSplashextendsSplashScreen {

    /**

     * Use your own splash image instead of thedefault one

     *

     * @return"/splash/javafx.png"

     */

    @Override

    public StringgetImagePath() {

        returnsuper.getImagePath();

    }

 

    /**

     * Customize if the splash screen should bevisible at all

     *

     * @return trueby default

     */

    @Override

    publicbooleanvisible() {

        returnsuper.visible();

    }}

最常用的就是这两个方法了,一个是更换照片,另一个是是否显示启动动画。

 

       4.0打包

       JavaFX-Maven-Plugin可以打包jarnativewebbundlekey-store这些。

如果是mac,一般用jarnative,执行命令mvn jfxnative会看到打包成了pkgdmg两种类型的安装包。并且都是在180M左右。这么大的包,主要是因为javajdk的问题,模块化之后就好了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 


猜你喜欢

转载自blog.csdn.net/zhangxingyu126/article/details/80528384
今日推荐