Java 8应用程序在MacOS Catalina 10.15上的暗模式下无法正常工作

时间:2019-11-13 16:40:36

标签: java macos macos-catalina macos-darkmode

如果用户将系统偏好设置:常规:外观切换为,则我应用程序的主屏幕(JFrame)将来自

enter image description here

enter image description here

因此,很明显,Java识别出已选择了暗模式,但是在进行相应的调整方面做得不好。最紧迫的问题是,文本颜色需要从黑色更改为白色,但是还有其他问题。

我使用的是Java 8的最新版本(jdk1.8.0_231.jdk),尚无法移至Java 13,但是从release notes听起来好像它们仍然在Dark模式下有问题。

是否有确定的方法可以解决此不确定的最佳方法。

还值得注意的是,JDialog子类不会变为黑色的其他页面(除了菜单栏,因此仍然可读,但看上去不合适)

例如来自

enter image description here

enter image description here

从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);
}

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

1 个答案:

答案 0 :(得分:0)

看起来Sun正在为此做准备,可能已经为Java 14准备好了,但是目前无法正常工作。

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

我通过设置UIManager颜色获得了一些成功,但是仍然存在问题,例如如果使用Aqua L&F,似乎无法从白色更改ComboBox或Button的颜色

还有其他由用户制作的L&Feel,例如DarculaVAqua,但它们带来了自己的问题。

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