首页 > 解决方案 > 更改系统外观主题

问题描述

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

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

并使用_

MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());

有没有办法改变系统外观的主题(亮/暗)?

系统默认值(在 Windows 上)光

系统黑暗(我希望实现的目标)黑暗的

我想要系统 L&F 的黑暗主题。

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

标签: javaswinglook-and-feel

解决方案


要改变系统的外观和感觉,我们必须了解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 的一些信息。
    您可以使用它在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);               
    } 
    

很少有更多信息来定制它。


推荐阅读