简体   繁体   English

更改系统外观主题

[英]Change system look and feel theme

I know I can set System L&F using我知道我可以使用设置 System L&F

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

and change the theme for a certain L&F ( say, "Metal" ) using并使用更改某个 L&F(例如“金属” )的主题

MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());

Is there a way to change the theme (light/dark) for the System Look and Feel?有没有办法更改系统外观的主题(浅色/深色)?

System Default (On Windows)系统默认值(在 Windows 上) 光

System Dark (What I wish to achieve)系统黑暗(我希望实现的目标) 黑暗的

I want the dark theme of the System L&F.我想要 System L&F 的黑暗主题。

If a dark theme does not exist, I wish to invert the colours of the default theme.如果不存在深色主题,我希望反转默认主题的颜色。

To change the system look and feel we have to understand how the current implementation of UIManager.getSystemLookAndFeelClassName() works.要更改系统外观,我们必须了解UIManager.getSystemLookAndFeelClassName()的当前实现是如何工作的。

The code in javax.swing.UIManager is 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();
    }

Now for Windows it gets its data from com.sun.java.swing.plaf.windows.WindowsLookAndFeel .现在对于 Windows,它从com.sun.java.swing.plaf.windows.WindowsLookAndFeel获取数据。 You can find more information about the class in this link ( Open Jdk WindowsLookAndFeel.java ).您可以在此链接( 打开 Jdk WindowsLookAndFeel.java )中找到有关该类的更多信息。

The link clearly says "Implements the Windows95/98/NT/2000 Look and Feel".该链接清楚地写着“实现了 Windows95/98/NT/2000 的外观和感觉”。 Package name starts with " com.sun.java.swing ".包名以“ com.sun.java.swing ”开头。 I guess chances for getting updates might be meagre and your system might have higher windows version.我想获得更新的机会可能很少,而且您的系统可能有更高的 Windows 版本。

To solve your issue we can use many ways为了解决您的问题,我们可以使用多种方法

  1. Create a new class then extend the above class and override the methods.创建一个新类,然后扩展上述类并覆盖方法。 Then you can change default colors.然后您可以更改默认颜色。

  2. By using Synth—the basis for creating your own look and feel with an XML file.通过使用 Synth — 使用 XML 文件创建您自己的外观和感觉的基础。

  3. Use Nimbus.使用 Nimbus。 As it is very easy to customize it.因为它很容易定制。 Some info on how to use Nimbus.关于如何使用 Nimbus 的一些信息。
    You can use it to set in UIManager.setLookAndFeel object.您可以使用它在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); }

Few more information to customize it.更多的信息来定制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM