JavaFX

References

Setup (Gradle + JavaFX)

📗 TIP
後來才知道 JetBrains 全家桶都幫你處理的 hosay hosay 了,
不過還是建議自己純手動體會一下,別忘了在做 CI/CD 時你還是要回歸 CLI build。

1. 安裝 gradle

  • Windows (scoop)
    ​​​​scoop install gradle
    

2. 創建 project

mkdir javafx-app
cd javafx-app
gradle init --type java-application

3. 調整設定

  • 接下來要拔除 Unit Testing,加入 JavaFX
  • src/test 目錄可以不要了
  • app/build.gradle.kts
    ​​​​plugins {
    ​​​​    java
    ​​​​    application
    ​​​​    id("org.javamodularity.moduleplugin") version "1.8.12"
    ​​​​    id("org.openjfx.javafxplugin") version "0.0.13"
    ​​​​    // javafxplugin:自動管理 JavaFX 依賴,只要在 javafx 欄位紀錄好相關資訊即可。
    ​​​​    // 這樣 implementation 欄位就不用一一列出用到的 javafx 模組。
    ​​​​}
    
    ​​​​repositories {
    ​​​​    mavenCentral()
    ​​​​}
    
    ​​​​dependencies {
    ​​​​    // ...
    ​​​​}
    
    ​​​​application {
    ​​​​    mainModule.set("org.example")
    ​​​​    mainClass.set("org.example.App")
    ​​​​}
    
    ​​​​java {
    ​​​​    toolchain {
    ​​​​        languageVersion = JavaLanguageVersion.of(23)
    ​​​​    }
    ​​​​}
    
    ​​​​javafx {
    ​​​​    version = "24.0.1"
    ​​​​    modules = listOf("javafx.controls", "javafx.fxml", "javafx.media")
    ​​​​}
    
    ​​​​tasks.withType<JavaCompile> {
    ​​​​    options.encoding = "UTF-8"
    ​​​​}
    
  • gradle.properties (因為 JavaFX 和 configuration cache 不相容)
    ​​​​org.gradle.configuration-cache=false
    

4. 範例

  • src/main/java/org/example/App.java
    ​​​​package org.example;
    
    ​​​​import javafx.application.Application;
    ​​​​import javafx.scene.Scene;
    ​​​​import javafx.scene.control.Button;
    ​​​​import javafx.scene.layout.StackPane;
    ​​​​import javafx.stage.Stage;
    
    ​​​​public class App extends Application {
    
    ​​​​    @Override
    ​​​​    public void start(Stage primaryStage) {
    ​​​​        // 創建按鈕
    ​​​​        Button btn = new Button("Click Me!");
    
    ​​​​        // 當按鈕被點擊時顯示訊息
    ​​​​        btn.setOnAction(event -> System.out.println("Hello, JavaFX!"));
    
    ​​​​        // 創建一個布局並將按鈕添加到布局中
    ​​​​        StackPane root = new StackPane();
    ​​​​        root.getChildren().add(btn);
    
    ​​​​        // 設定場景並顯示
    ​​​​        Scene scene = new Scene(root, 300, 250);
    ​​​​        primaryStage.setTitle("Hello JavaFX!");
    ​​​​        primaryStage.setScene(scene);
    ​​​​        primaryStage.show();
    ​​​​    }
    
    ​​​​    public static void main(String[] args) {
    ​​​​        launch(args);
    ​​​​    }
    ​​​​}
    

5. 執行

  • build (有新類別出現時,記得重新 build 一次)
    ​​./gradlew clean build
    
  • run
    ​​./gradlew run
    

6. 打包

  • 打包成執行檔 .exe
    • 打包目錄:app\build\jpackage\app
      • 除了執行檔以外,會有一些 runtime 依賴,不能刪
    • app/build.gradle.kts
      ​​​​plugins {
      ​​​​    // ...
      ​​​​    id("org.beryx.jlink") version "2.25.0" // ✅
      ​​​​}
      ​​​​application {
      ​​​​    // ...
      ​​​​    mainModule = "org.example" // ✅
      ​​​​}
      ​​​​jlink { // ✅
      ​​​​    imageName = "ExecutableName"
      ​​​​    jpackage {
      ​​​​        skipInstaller = true
      ​​​​        appVersion = "1.0.0"
      ​​​​    }
      ​​​​}
      
    • app\src\main\java\module-info.java
      ​​​​module org.example { // ✅
      ​​​​    requires javafx.base;
      ​​​​    requires javafx.controls;
      ​​​​    requires javafx.fxml;
      ​​​​    requires transitive javafx.graphics;
      
      ​​​​    opens org.example to javafx.fxml;
      
      ​​​​    exports org.example;
      ​​​​}
      
    • 打包
      ​​​​./gradlew jpackage
      
  • 打包成安裝檔 .msi

Directory Structure

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Artifacts

模組名稱 Maven Artifact 名稱 用途說明
javafx-base org.openjfx:javafx-base JavaFX 的核心功能(物件模型、事件處理等)
javafx-graphics org.openjfx:javafx-graphics 繪圖、場景圖(Scene Graph)、動畫等
javafx-controls org.openjfx:javafx-controls UI 控制項(按鈕、表格、文字框等)
javafx-fxml org.openjfx:javafx-fxml 處理 FXML 檔案(MVC 設計)
javafx-media org.openjfx:javafx-media 播放音訊與影片
javafx-web org.openjfx:javafx-web WebView 元件,可嵌入網頁
javafx-swing org.openjfx:javafx-swing 在 JavaFX 中使用 Swing 元件(反過來整合)
javafx-swt org.openjfx:javafx-swt 整合 Eclipse SWT 元件