idea javafx添加maven_JavaFx系列教程之一:JavaFx+Springboot+Maven 开发打包教程

最近在用 JavaFx 开发窗体程序,并且以后打算把所有的窗体软件切换到 JavaFx 上面,并且还想用 springboot 的__各种好处,集成了 springboot 就可以愉快的用各种服务了。所以就有了这个博客。。。

我本来想自己写一个 javafx-spring-boot-starter 的,其原理也是很简单地,利用 springboot的 aware 就可以了。但是发现德国这哥们已经写了一个,就变懒了。哎,没办法,我真的是太懒了。

本次的代码Github 地址:https://github.com/spartajet/...

1. 介绍

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

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

2. Maven 配置

maven 主要配置依赖库和插件,具体如下:

org.springframework.boot

spring-boot-starter-actuator

${spring.boot.version}

org.springframework.boot

spring-boot-starter

${spring.boot.version}

org.springframework.boot

spring-boot-starter-log4j2

${spring.boot.version}

org.springframework.boot

spring-boot-starter-test

${spring.boot.version}

test

de.roskenet

springboot-javafx-support

${springboot-javafx-support.version}

org.springframework.boot

spring-boot-maven-plugin

com.zenjava

javafx-maven-plugin

com.spartajet.fxboot.demo.MainController

Spartajet

其中,比较重要的是:com.spartajet.fxboot.demo.MainController 这个是打包的时候的 main 类。Spartajet是组织名称。

3. Javafx 集成springboot

3.1 创建 FXML 布局文件

可以用 SceneBuilder 工具创建 FXML 文件,我还是建议自己写 FXML,刚开始可能不习惯,慢慢习惯就好了。但是,我墙裂建议布局和样式分开,fxml 只管布局,css 只管样式。

3.2 MainStageController

每一个 fxml 布局文件对应一个 controller,要在 fx:controller 里面设置。

/**

* @description

* @create 2017-05-20 下午1:55

* @email spartajet.guo@gmail.com

*/

@FXMLController

public class MainStageController implements Initializable {

/**

* Called to initialize a controller after its root element has been

* completely processed.

*

* @param location The location used to resolve relative paths for the root object, or

* null if the location is not known.

* @param resources The resources used to localize the root object, or null if

*/

public void initialize(URL location, ResourceBundle resources) {

}

}

实现Initializable接口,加上@FXMLController注解,其实很简单的。看看@FXMLController的源码:

@Component

@Retention(RetentionPolicy.RUNTIME)

public @interface FXMLController {

}

3.3 MainStageView

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

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

public class MainStageView extends AbstractFxmlView {

}

添加的是注解@FXMLView,源码如下:

@Component

@Retention(RetentionPolicy.RUNTIME)

public @interface FXMLView {

String value() default "";

String[] css() default {};

String bundle() default "";

}

从这里也能看到,可以在这里注入 css 样式文件以及 bundle 文件。还是比较方便的,我是在 fxml 里面注入的 css 文件,都是可以的。

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

3.3 MainController

@SpringBootApplication

public class MainController extends AbstractJavaFxApplicationSupport {

/**

* The entry point of application.

*

* @param args the input arguments

*/

public static void main(String[] args) {

launchApp(MainController.class, MainStageView.class, args);

}

/**

* Start.

*

* @param stage the stage

*

* @exception Exception the exception

*/

@Override

public void start(Stage stage) throws Exception {

super.start(stage);

}

}

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

可以看到

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

3.4 启动动画

/**

* @description

* @create 2017-05-20 下午2:54

* @email spartajet.guo@gmail.com

*/

public class CustomSplash extends SplashScreen {

/**

* Use your own splash image instead of the default one

*

* @return "/splash/javafx.png"

*/

@Override

public String getImagePath() {

return super.getImagePath();

}

/**

* Customize if the splash screen should be visible at all

*

* @return true by default

*/

@Override

public boolean visible() {

return super.visible();

}

}

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

4.0 打包

JavaFX-Maven-Plugin可以打包 jar、native、webbundle、key-store这些。

由于我是 mac,一般用 jar和 native,执行命令mvn jfx:native 会看到打包成了pkg 和 dmg 两种类型的安装包。并且都是180M 左右。由于没有 windows 计算机,所以还不知道打包成 exe 的状态。这么大的包,主要是因为 java 的 jdk 问题,模块化以后就好了。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值