java - 更改系统外观主题
问题描述
我知道我可以使用设置系统 L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
并使用_
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
有没有办法改变系统外观的主题(亮/暗)?
我想要系统 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 版本。
为了解决您的问题,我们可以使用多种方法
创建一个新类,然后扩展上述类并覆盖方法。然后您可以更改默认颜色。
通过使用 Synth — 使用 XML 文件创建您自己的外观和感觉的基础。
使用雨云。因为它很容易定制。关于如何使用 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); }
很少有更多信息来定制它。
推荐阅读
- php - Facebook SDK 访问令牌和 ajax
- google-cloud-platform - 无法对深度学习图像进行 sudo
- java - 使用先前选定的数据错误重新加载活动
- linux - 特定目录中的 wget 超时,但在另一个目录中执行时不会
- c# - 如何使用 Linq lambda 表达式在两个列表之间获得相等和不相等的值
- node.js - 将 semver 版本与 tilde / caret 进行比较
- javascript - 使用 angularjs 承诺的随机数总和
- jquery - 阻止了来源为“http://localhost”的帧
- swt - 单击swt中的按钮时如何创建文本框?
- url-rewriting - 用大量参数重写很长的 URL