下面的答案是@Stijn 先前答案的扩展,变化在于文档中建议的初始化方法。引用链接nimbuslaf和swing 教程 - 大小
版本说明:不要通过调用 UIManager.setLookAndFeel 方法显式设置 Nimbus 外观,因为并非 Java SE 6 的所有版本或实现都支持 Nimbus。此外,Nimbus 包的位置在 JDK 6 Update 10 和 JDK 7 版本之间发生了变化。遍历所有已安装的外观实现是一种更强大的方法,因为如果 Nimbus 不可用,则使用默认外观。
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
try {
Constructor c = Class.forName("MyStyleFactory").getConstructor(String.class);
c.newInstance("small"); // regular, mini, small or large
} catch (ExceptionInInitializerError eiie){
//
} catch (LinkageError le){
//
} catch (ClassNotFoundException cnfe){
//
}
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
文件:MyStyleFactory.java
public class MyStyleFactory extends SynthStyleFactory {
protected static String variant = "regular";
final SynthStyleFactory styleFactory = SynthLookAndFeel.getStyleFactory();
static {
SynthLookAndFeel.setStyleFactory(new MyStyleFactory(variant));
}
public MyStyleFactory(String variant) {
if (variant.equals("regular") || variant.equals("mini")
|| variant.equals("small") || variant.equals("large"))
MyStyleFactory.variant = variant;
}
@Override
public SynthStyle getStyle(JComponent c, Region id) {
c.putClientProperty("JComponent.sizeVariant", variant);
return styleFactory.getStyle(c, id);
}
}