注意:这不是问题的答案(这是在哪里设置 LAF)。相反,它回答了如何以独立于其包名称的方式设置 LAF 的问题。在类被移动的情况下简化生活,如从 com.sun* 到 javax.swing 的 fi Nimbus。
基本方法是向 UIManager 查询其已安装的 LAF,循环遍历它们直到找到匹配项并设置它。这是在 SwingX 中实现的方法:
/**
* Returns the class name of the installed LookAndFeel with a name
* containing the name snippet or null if none found.
*
* @param nameSnippet a snippet contained in the Laf's name
* @return the class name if installed, or null
*/
public static String getLookAndFeelClassName(String nameSnippet) {
LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
for (LookAndFeelInfo info : plafs) {
if (info.getName().contains(nameSnippet)) {
return info.getClassName();
}
}
return null;
}
用法(这里没有异常处理)
String className = getLookAndFeelClassName("Nimbus");
UIManager.setLookAndFeel(className);