gpt4 book ai didi

java - 更改系统外观主题

转载 作者:行者123 更新时间:2023-12-04 14:55:02 26 4
gpt4 key购买 nike

我知道我可以设置系统 L&F 使用

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

并使用

更改特定 L&F 的主题( 比如“金属”)
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());

有没有办法改变系统外观的主题(浅色/深色)?

系统默认值(在 Windows 上)Light

系统暗(我希望实现的)Dark

我想要系统 L&F 的深色主题。

如果不存在深色主题,我希望反转默认主题的颜色。

最佳答案

要更改系统外观,我们必须了解 UIManager.getSystemLookAndFeelClassName() 的当前实现是如何工作的。

javax.swing.UIManager中的代码是

 public static String getSystemLookAndFeelClassName() {
String systemLAF = AccessController.doPrivileged(
new GetPropertyAction("swing.systemlaf"));
if (systemLAF != null) {
return systemLAF;
}
OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());
if (osType == OSInfo.OSType.WINDOWS) {
return "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else {
String desktop = AccessController.doPrivileged(new GetPropertyAction("sun.desktop"));
Toolkit toolkit = Toolkit.getDefaultToolkit();
if ("gnome".equals(desktop) &&
toolkit instanceof SunToolkit &&
((SunToolkit) toolkit).isNativeGTKAvailable()) {
// May be set on Linux and Solaris boxs.
return "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
}
if (osType == OSInfo.OSType.MACOSX) {
if (toolkit.getClass() .getName()
.equals("sun.lwawt.macosx.LWCToolkit")) {
return "com.apple.laf.AquaLookAndFeel";
}
}
if (osType == OSInfo.OSType.SOLARIS) {
return "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}
}
return getCrossPlatformLookAndFeelClassName();
}

现在对于 Windows,它从 com.sun.java.swing.plaf.windows.WindowsLookAndFeel 获取数据。您可以在此链接 (Open Jdk WindowsLookAndFeel.java) 中找到有关该类的更多信息。

该链接清楚地写着“实现 Windows95/98/NT/2000 外观”。包名称以“com.sun.java.swing”开头。我想获得更新的机会可能微乎其微,而且您的系统可能具有更高的 Windows 版本。

要解决您的问题,我们可以使用多种方法

  1. 创建一个新类,然后扩展上面的类并覆盖方法。然后您可以更改默认颜色。

  2. 通过使用 Synth——使用 XML 文件创建您自己的外观的基础。

  3. 使用 Nimbus。因为定制它非常容易。有关如何使用 Nimbus 的一些信息。
    您可以使用它在 UIManager.setLookAndFeel 对象中进行设置。

    try { 
    UIManager.put( "control", new Color( 0, 0, 0) );
    UIManager.put( "Button.background", new Color(18, 30, 49) );
    UIManager.put( "Button.foreground", new Color( 59, 68, 75) );
    UIManager.put( "info", new Color(128,128,128) );
    UIManager.put( "nimbusBase", new Color( 18, 30, 49) );
    UIManager.put( "nimbusAlertYellow", new Color( 248, 187, 0) );
    UIManager.put( "nimbusDisabledText", new Color( 128, 128, 128) );
    UIManager.put( "nimbusFocus", new Color(115,164,209) );
    UIManager.put( "nimbusGreen", new Color(176,179,50) );
    UIManager.put( "nimbusInfoBlue", new Color( 66, 139, 221) );
    UIManager.put( "nimbusLightBackground", new Color( 18, 30, 49) );
    UIManager.put( "nimbusOrange", new Color(191,98,4) );
    UIManager.put( "nimbusRed", new Color(169,46,34) );
    UIManager.put( "nimbusSelectedText", new Color( 255, 255, 255) );
    UIManager.put( "nimbusSelectionBackground", new Color( 104, 93, 156) );
    UIManager.put( "text", new Color( 255, 255, 255) );
    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    break;
    }
    }
    } catch (Exception e) {
    System.err.println("Exception caught:"+e);
    }

自定义它的更多信息。

关于java - 更改系统外观主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68222283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com