https://wiki.vnoj.io.vn

Gradle JavaFX Java Desktop Application

Gradle JavaFX Java Desktop Application

1. File settings.gradle


rootProject.name = 'GradleFX'
include('app')
project(":app").name = "GradleJavaFX"

2. File build.gradle


plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.11'
}

repositories {
    mavenCentral()
}

def currentOS = org.gradle.internal.os.OperatingSystem.current()
def platform

if (currentOS.isWindows()) {
    platform = 'win'
}else if (currentOS.isMacOsX()) {
    platform = 'mac'
}else{
    platform = 'linux'
}

javafx {     
    version = "18-ea+10"
    modules = ['javafx.controls']   
}

dependencies {
    runtimeOnly "org.openjfx:javafx-graphics:18-ea+10:${platform}"
    runtimeOnly "org.openjfx:javafx-base:18-ea+10:${platform}"
    runtimeOnly "org.openjfx:javafx-controls:18-ea+10:${platform}"
    runtimeOnly "org.openjfx:javafx-fxml:18-ea+10:${platform}"
    runtimeOnly "org.openjfx:javafx-swing:18-ea+10:${platform}"
    runtimeOnly "org.openjfx:javafx-media:18-ea+10:${platform}"
    runtimeOnly "org.openjfx:javafx-web:18-ea+10:${platform}"
}

mainClassName = 'GradleFX.App'

application {
    mainClass = mainClassName
}

3. Class UtilAlert


import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;

public class UtilAlert {

    private String Title = "";
    private String HeaderText = "";

    public UtilAlert(String Title, String HeaderText) {
        this.Title = Title;
        this.HeaderText = HeaderText;
    }

    public UtilAlert() {}

    public ButtonType showAndWait(Alert.AlertType at, String string, ButtonType... bts) {
        Alert alert = new Alert(at, string, bts);
        alert.setTitle(this.Title);
        alert.setHeaderText(this.HeaderText);
        return alert.showAndWait().get();
    }

    public void Show(Alert.AlertType at, String string, ButtonType... bts) {
        Alert alert = new Alert(at, string, bts);
        alert.setTitle(this.Title);
        alert.setHeaderText(this.HeaderText);
        alert.show();
    }
}

4. Class frmMain


package GradleFX;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.*;

public class frmMain extends Application {

    @Override
    public void start(Stage primaryStage) {

        UtilAlert alert = new UtilAlert("Thoát khỏi hệ thống", "Xác nhận thoát");

        Label l = new Label("Hello JavaFX");
        l.setFont(new Font("Times New Roman", 18));

        Scene scene = new Scene(new StackPane(l), 800, 600);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Gradle JavaFX");
        primaryStage.setMaximized(true);
        primaryStage.show();

        primaryStage.setOnCloseRequest(e -> {
            if (alert.showAndWait(Alert.AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO) != ButtonType.YES) {
                e.consume();
            } else {
                Platform.exit();
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

5. Class App


package GradleFX;

public class App {
    public static void main(String[] args) {
        frmMain.main(args);
    }
}