我尝试更改nimbus JProgressBar的默认颜色。我可以通过更改“nimbusOrange”来更改颜色全局。我喜欢这个结果。但我需要一个由nimbus在原始nimbusOrange中的ProgressBar和另一个包含在蓝色相同程序中的ProgressBar。我试试这个:
JProgressBar progressBar = new JProgressBar();
UIDefaults overrides = new UIDefaults();
overrides.put("nimbusOrange", Color.BLUE);
overrides.put("ProgressBar[Enabled].backgroundPainter", UIManager.get("ProgressBar[Enabled].backgroundPainter"));
//overrides.put("ProgressBar[Enabled].foregroundPainter", UIManager.get("ProgressBar[Enabled].foregroundPainter"));
progressBar.putClientProperty("Nimbus.Overrides", overrides);
progressBar.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
但是,如果我将foregroundPainter放回代码,它只显示ProgressBar的正常橙色样式......
任何帮助?
亲切的问候 Highchiller
答案 0 :(得分:0)
好的,我得到了解决方案,但它看起来很糟糕......如果有人知道更简单的解决方法,请告诉我。
我采用扩展AbstractRegionPainter的原始ProgressBarPainter.java。我将名称更改为BlueProgressBarPainter。它还在构造函数中获取PaintContext对象。此类是AbstractRegionPainter的受保护内部类。所以我将给定参数更改为Object并将其强制转换为构造函数。
毕竟构造函数看起来像:
public BlueProgressBarPainter(Object ctx, int state) {
super();
this.state = state;
this.ctx = (AbstractRegionPainter.PaintContext)ctx;
}
此外,我们将“nimbusOrange”的所有访问权限更改为“internalBlue”,并在引入新的LookAndFeel之前将此新颜色(例如BLUE)添加到nimbus和该行:
UIManager.put("internalBlue", Color.BLUE);
好的......还好。 现在我们必须将自己的画家添加到特定的progressBar中。但是我们需要原画家的AbstractRegionPainter.PaintContext对象。为此,我们需要反思!导致AbstractRegionPainter中的方法“getPaintContext()”受到保护。
// initialize progressBar
JProgressBar progressBar = new JProgressBar();
// create own UIDefaults
UIDefaults overrides = new UIDefaults();
BlueProgressBarPainter painter = null;
// get the original AbstractRegionPainter of Nimbus
AbstractRegionPainter abstractPainter = (AbstractRegionPainter)UIManager.get("ProgressBar[Enabled].foregroundPainter");
// reflection begin
try {
Class<?> clazz = abstractPainter.getClass();
// get the protected Method of AbstractRegionPainter
Method protectedMethod = clazz.getDeclaredMethod("getPaintContext");
protectedMethod.setAccessible(true);
// create now our own Painter with the original AbstractRegionPainter.PaintContext
painter = new BlueProgressBarPainter(
(Object) protectedMethod.invoke(abstractPainter) ,
3 ); // 3 is constant for "FOREGROUND_ENABLED"
} catch (Exception e) {}
// set original background painter
overrides.put("ProgressBar[Enabled].backgroundPainter", UIManager.get("ProgressBar[Enabled].backgroundPainter"));
// set own foreground painter
overrides.put("ProgressBar[Enabled].foregroundPainter", painter);
// accept overrides
progressBar.putClientProperty("Nimbus.Overrides", overrides);
progressBar.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
我觉得这很难......但它对o.O呜呜很有用。
祝你好运, Highchiller