SpringBoot对于Java程序员来说可以是一个福音,它让程序员在开发的时候,大大简化了各种spring的xml配置。
那么在JavaFX项目使用SpringBoot会是怎么样的体验呢?
这次使用“GuyHub”的开源项目springboot-javafx-support体验一下SpringBoot + JavaFX开发。
JavaFX桌面应用开发系列文章传送门 ~
采用最新的SpringBoot版本和springboot-javafx-support版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>de.roskenet</groupId>
<artifactId>springboot-javafx-support</artifactId>
<version>2.1.6</version>
</dependency>
springboot-javafx-support 主要两个关键的组件:
视图排版
这里使用FXML布局界面。
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="99.0" layoutY="109.0" prefHeight="34.0" prefWidth="394.0" text="Hello World!">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</Pane>
视图类
视图类很简单,只需要继承AbstractFxmlView并添加@FXMLView注解即可。
@FXMLView
public class HelloworldView extends AbstractFxmlView {
}
启动类
启动类跟SpringBoot的启动类差不多,不过需要需要继承AbstractJavaFxApplicationSupport,然后将主视图类传进去。
@SpringBootApplication
public class App extends AbstractJavaFxApplicationSupport {
public static void main(String[] args) {
launch(App.class, HelloworldView.class, args);
}
}
这样一个超级简单的SpringBoot + JavaFX就完成了。
springboot-javafx-support默认带了启动界面,这在SpringBoot项目中是非常有必要的,因为SpringBoot项目通常来说启动都要一定的时间。
程序启动完成后,启动界面会自动关闭,显示程序主界面。
springboot-javafx-support 这个框架及其简单,所有源码总共才9个类,下面介绍一下这9个类。
springboot-javafx-support中有一些约定:
当然这些约定可以通过@FXMLView注解中的属性来调整。
public @interface FXMLView {
String value() default "";
String[] css() default {};
String bundle() default "";
String encoding() default "ISO-8859-1";
String title() default "";
String stageStyle() default "UTILITY";
}
到目前为止还看不到springboot-javafx-support中spring的影子,接下来通过一个例子来展现一下springboot-javafx-support的魅力。
这里通过两个视图切换作为例子:
调整之前的视图,新增调整到Hi视图的按钮,并将视图跟ViewController绑定,处理toHi事件。
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="example.ViewController">
<children>
<Label layoutX="99.0" layoutY="109.0" prefHeight="34.0" prefWidth="394.0" text="Hello World!">
<font>
<Font size="26.0" />
</font>
</Label>
<Button layoutX="99.0" layoutY="169.0" prefHeight="34.0" prefWidth="150.0" text="Hi" onAction="#toHi"/>
</children>
</Pane>
同样的Hi视图也绑定到ViewController中
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="example.ViewController">
<children>
<Label layoutX="99.0" layoutY="109.0" prefHeight="34.0" prefWidth="394.0" text="Hi IT青年!">
<font>
<Font size="26.0" />
</font>
</Label>
<Button layoutX="99.0" layoutY="169.0" prefHeight="34.0" prefWidth="150.0" text="Hello" onAction="#toHello"/>
</children>
</Pane>
这里跟之前写过的一些案例有点不同的时候,fxml和controller的关系是多对一的关系。(之前的案例都是一对一的关系)。
springboot-javafx-support组件的关系图如下:
ViewController中,将Helloworld和Hi视图注入进来,单点击按钮的时候进行视图切换。
ViewController源码如下:
@FXMLController
public class ViewController {
@Autowired
private HelloworldView helloworldView;
@Autowired
private HiView hiView;
@Autowired
private FooService fooService;
public void toHi() {
System.out.println(fooService.service());
App.getScene().setRoot(hiView.getView());
}
public void toHello() {
App.getScene().setRoot(helloworldView.getView());
}
}
这里的@Autowired是Spring的注解,由于@FXMLController主键的Controller是Spring的bean,所以这里完全可以像spring开发一样编写代码。
如:@Autowired private FooService fooService;
FoorService是一个简单的接口
public interface FooService {
String service();
}
其实现:
@Service
public class FooServiceImpl implements FooService {
@Override
public String service() {
return "Foo Service";
}
}
springboot-javafx-support提供了默认的启动界面,当然也开放了自定义界面的入库。
自定义启动节目只需要继承SplashScreen并重写相关方法即可,这里简单的替换启动图案:
public class CustomLoadingView extends SplashScreen {
@Override
public String getImagePath() {
return "/images/logo.png";
}
}
在启动类中指定启动界面:
@SpringBootApplication
public class App extends AbstractJavaFxApplicationSupport {
public static void main(String[] args) {
launch(App.class, HelloworldView.class, new CustomLoadingView(), args);
}
}
这样启动界面就不再是之前的样子
springboot-javafx-support提供了一些案例,包括:
等等。
有兴趣的话可以看看,GitHub传送门
https://github.com/roskenet/spring-javafx-examples
另外附送 springboot-javafx-support 的GitHub地址
https://github.com/roskenet/springboot-javafx-support
=========================================================
关注 公众号 “HiIT青年” 发送 “javafx-springboot” 获取源码。(如果没有收到回复,可能是你之前取消过关注。)
关注公众号,阅读更多文章。
原网址: 访问
创建于: 2024-02-18 11:42:19
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论