源码介绍

build.gradle 内容如下:

plugins {
    id 'java'
    id('application')
}

group = 'top.yunp'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation('com.formdev:flatlaf:3.3')
    implementation 'com.github.Dansoftowner:jSystemThemeDetector:3.6'
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
}

application{
    mainClass="top.yunp.sstws.Main"
}

使用的侦听操作系统主题变化的类是com.jthemedetecor.OsThemeDetector,主要示例代码如下:

package top.yunp.sstws;

import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.jthemedetecor.OsThemeDetector;

public class Main {

    private static OsThemeDetector themeDetector;

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

        new FirstView();
    }

    private static void syncTheme() {
        if (themeDetector.isDark()) {
            FlatDarkLaf.setup();
        } else {
            FlatLightLaf.setup();
        }
        FlatLaf.updateUI();
    }

    private static void configTheme() {
        System.setProperty("apple.awt.application.appearance", "system");
        themeDetector = OsThemeDetector.getDetector();
        themeDetector.registerListener(isDark -> {
            syncTheme();
        });
        syncTheme();
    }
}