Java 8 个应用程序无法在 MacOS Catalina 10.15 上使用深色模式正常工作

Java 8 application not working correctly with dark mode on MacOS Catalina 10.15

如果用户将 System Preferences:General:Appearance 切换到 Dark,我的应用程序的主屏幕(JFrame)将从

很明显 Java 认识到已选择深色模式,但没有很好地进行相应的调整。最紧迫的问题是文本颜色需要从黑色更改为白色,但还有其他问题。

我正在使用最新版本的 Java 8 (jdk1.8.0_231.jdk),还不能移动到 Java 13,但是从 release notes 听起来他们在黑暗模式下仍然存在问题。

是否有解决此问题的既定方法我不确定最佳方法。

另外值得注意的是,子类 JDialog 没有更改为黑色的其他页面(除了菜单栏,因此仍然可读但看起来不合适)

例如来自

开始使用 UIDefaults

public static boolean isDarkModeEnabled()
    {

        try
        {
            // check for exit status only. Once there are more modes than "dark" and "default", we might need to analyze string contents..
            final Process proc = Runtime.getRuntime().exec(new String[]{"defaults", "read", "-g", "AppleInterfaceStyle"});
            proc.waitFor(100, TimeUnit.MILLISECONDS);
            boolean result = proc.exitValue() == 0;
            MainWindow.logger.severe("Is Dark Mode:" + result);
            return result;
        }
        catch (IOException | InterruptedException | IllegalThreadStateException ex)
        {
            // IllegalThreadStateException thrown by proc.exitValue(), if process didn't terminate
            MainWindow.logger.severe("Unable to determine dark mode");
            return false;
        }

public static void displayAsDarkMode()
{
    Color whitish       = new Color(250,250, 250);
    Color fadedWhite    = new Color(200,200, 200);
    Color lightGray     = new Color(49,52, 56);
    Color darkGray      = new Color(30,34,38);

    UIManager.put("Label.foreground", whitish);
    UIManager.put("Panel.background", lightGray);
    UIManager.put("CheckBox.textForeground", whitish);
    UIManager.put("CheckBox.foreground", whitish);
    UIManager.put("TextField.background", darkGray);
    UIManager.put("TextField.foreground", whitish);
    UIManager.put("ComboBox.foreground", darkGray);
    UIManager.put("ComboBox.textForeground", whitish);
    UIManager.put("Button.background", lightGray);
    UIManager.put("Button.textForeground", fadedWhite);
    UIManager.put("List.background", darkGray);
    UIManager.put("List.foreground", whitish);
    UIManager.put("TextArea.background", darkGray);
    UIManager.put("TextArea.inactiveBackground", darkGray);
    UIManager.put("Table.background", darkGray);
    UIManager.put("Table.gridColor", lightGray);
}

但我知道我不是第一个遇到这个问题的人

看起来 Sun 正在处理这个问题,可能会在 Java 14 之前准备好,但目前无法正常工作。

https://bugs.openjdk.java.net/browse/JDK-8231438

我通过设置 UIManager 颜色取得了一些成功,但问题仍然存在,例如如果使用 Aqua L&F 似乎无法将组合框或按钮的颜色从白色更改

有其他用户制作的 L&Feel,例如 Darcula and VAqua,但这些都会带来自己的问题。

因此,在 Oracle 正确修复问题之前,我可能会使用我的部分 UIManager 方法。