javafx
基于Java
的桌面程序的开发技术
阅读此文档要求了解JavaFX
的的基本结构
此文档含项目创建
,代码介绍
,构建安装包
创建项目
- 创建
maven
项目,调整pom.xml
依赖
org.springframework.boot
spring-boot-starter-parent
2.1.9.RELEASE
org.springframework.boot
spring-boot-starter
de.roskenet
springboot-javafx-support
2.1.6
创建视图类
与视图文件
(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
备注: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
用例
org.springframework.boot
spring-boot-maven-plugin
com.zenjava
javafx-maven-plugin
com.podigua.translate.TranslateApplication
${project.build.finalName}
false
true
公司
说明
启动类
- 启动类需要继承
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 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
- mac:在
- 其他配置信息可以点击属性查看说明