JavaFX 结合 spring boot


javafx 基于Java的桌面程序的开发技术

阅读此文档要求了解JavaFX的的基本结构

此文档含项目创建,代码介绍,构建安装包

创建项目

  • 创建maven项目,调整pom.xml依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>de.roskenet</groupId> <artifactId>springboot-javafx-support</artifactId> <version>2.1.6</version> </dependency> </dependencies> 

创建视图类视图文件(fxml)

视图类

package com.example.view; import de.felixroske.jfxsupport.AbstractFxmlView; import de.felixroske.jfxsupport.FXMLView; @FXMLView(value = "/view/demo.fxml") public class DemoView extends AbstractFxmlView { } 

fxml

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.layout.FlowPane?> <FlowPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.example.controller.DemoController"> <children> <Button fx:id="button" onAction="#click" text="测试按钮" /> </children> </FlowPane> 

备注:javafx 启动过程较慢,此功能默认会加载一个动画

动画类

package com.example.splash; import de.felixroske.jfxsupport.SplashScreen; public class DemoSplash extends SplashScreen { @Override public boolean visible() { return super.visible(); } @Override public String getImagePath() { return super.getImagePath(); } } 

启动类

package com.example; import com.example.splash.DemoSplash; import com.example.view.DemoView; import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport; import javafx.stage.Stage; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class DemoApplication extends AbstractJavaFxApplicationSupport { public static void main(String[] args) { launch(DemoApplication.class, DemoView.class,new DemoSplash(), args); } @Override public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) { stage.setTitle("标题"); stage.setWidth(300); stage.setHeight(300); } } 

构建pom.xml

<build> <finalName>用例</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.zenjava</groupId> <artifactId>javafx-maven-plugin</artifactId> <configuration> <mainClass>com.podigua.translate.TranslateApplication</mainClass> <appName>${project.build.finalName}</appName> <needMenu>false</needMenu> <needShortcut>true</needShortcut> <vendor>公司</vendor> </configuration> </plugin> </plugins> </build> 

说明

启动类

  • 启动类需要继承AbstractJavaFxApplicationSupport
/** * 1:启动类 * 2:视图类 * 3:启动动画类(SplashScreen) */ public static void launch(Class<? extends Application> appClass, Class<? extends AbstractFxmlView> view, SplashScreen splashScreen, String[] args){ } /** * 此处修改窗体信息 * @param stage * @param ctx */ @Override public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) { stage.setTitle("标题"); stage.setWidth(300); stage.setHeight(300); } /** * 窗体图标 */ public Collection<Image> loadDefaultIcons() { return Arrays.asList(new Image("图片")); } 

视图类

@FXMLView(value = "/view/demo.fxml") value=fxml的路径,以/开头 

控制类

@FXMLController public class DemoController implements Initializable{ } 

启动动画

/** * 动画路径 */ @Override public String getImagePath() { return super.getImagePath(); } /** *是否显示动画 */ @Override public boolean visible() { return false; } 

效果

 
效果图

打包

  • 使用mvn jfx:native 进行打包
  • 如何设置程序图标
    • mac:在src/main/deploy/package/macosx/${appName}.icns
    • windows:在src/main/deploy/package/windows/${appName}.ico
  • 其他配置信息可以点击属性查看说明
  
转自:https://www.jianshu.com/p/bb48beb431e6 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM