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
- mac:在
- 其他配置信息可以點擊屬性查看說明