如果用户将System Preferences:General:Appearance切换为Dark我的应用程序的主屏幕(一个 JFrame)来自
到
很明显,Java 认识到选择了深色模式,但并没有很好地进行相应的调整。最紧迫的问题是文本颜色需要从黑色变为白色,但还有其他问题。
我正在使用最新版本的 Java 8 (jdk1.8.0_231.jdk),还不能迁移到 Java 13,但从发行说明中听起来他们仍然存在暗模式问题。
有没有固定的方法来解决这个我不确定最好的方法。
另外值得注意的是其他页面的子类 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);
}
但我知道我不是第一个遇到这个问题的人