我使用swing + nimbus对组件进行样式设置。我想在运行时使用“ Nimbus.Overrides”更改组件的样式。
private void SetExceptionState() {
//password.setBackground(new Color(200,0,0,120));
UIDefaults overrides = new UIDefaults();
overrides.put("PasswordField.background", Color.red);
password.putClientProperty("Nimbus.Overrides", overrides);
password.revalidate();
password.updateUI();
}
private void ResetExceptionState() {
//password.setBackground(Color.white);
UIDefaults overrides = new UIDefaults();
overrides.put("PasswordField.background", Color.white);
password.putClientProperty("Nimbus.Overrides", overrides);
}
我第一次设置重写时,可以使用SetExceptionState()方法说它起作用。我得到红色背景。第二次使用此功能时,没有任何反应。似乎,覆盖仅被评估一次。
我要介绍的是密码字段的新状态,并设置其不同的样式。有可能这样做吗?
最好的祝福,
伊格德拉西尔
是的,这是有可能的,并且Nimbus实际上会侦听“ Nimbus.Overrides”的更改-只是:它不会卸载某些属性(如果有的话)!instanceof UIResource
,至少是背景,前景,字体(也是其他情况)
在您的上下文中,您确实最初安装了RED的非uiresource,有效地告诉了laf不要再次接触它-并且它符合:-)
我可以使之起作用的唯一方法是在设置新的替代值之前将背景设为空,例如:
private void setExceptionState(JComponent password) {
password.setBackground(null);
UIDefaults overrides = new UIDefaults();
overrides.put("PasswordField.background", Color.RED);
password.putClientProperty("Nimbus.Overrides", overrides);
}
private void resetExceptionState(JComponent password) {
password.setBackground(null);
UIDefaults overrides = new UIDefaults();
overrides.put("PasswordField.background", Color.WHITE);
password.putClientProperty("Nimbus.Overrides", overrides);
}
更新
实际上,以上内容并未回答真正的问题:
引入密码字段的新状态并设置不同的样式
Nimbus确实的确允许添加自定义状态(尽管结局有些难以预测,就像Synth那个不受人爱的最小的孩子一样;-)前进的道路是
所有这些配置都必须在安装LAF之后且实例化第一个JPasswordField之前完成,如果LAF在运行时切换,很可能(没有测试)会带来问题。
protected void installCustomPasswordFieldState() {
// implement a custom state
State<JPasswordField> state = new State<JPasswordField>("Invalid") {
@Override
protected boolean isInState(JPasswordField c) {
Object invalid = c.getClientProperty("Invalid");
return Boolean.TRUE.equals(invalid);
}
};
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
// register available states
// note: couldn't find a way to grab the already available states
// so this is guesswork
defaults.put("PasswordField.States", "Enabled, Focused, Invalid");
// install the custom state
defaults.put("PasswordField.Invalid", state);
// install the properties for the custom state
// note: background has no effect
defaults.put("PasswordField[Invalid].background",
Color.RED);
javax.swing.Painter<JComponent> p = new javax.swing.Painter<JComponent>() {
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(Color.RED);
// this is crude - overpainting the complete area, do better!
g.fillRect(0, 0, width, height);
}
};
// using a painter has an effect
defaults.put("PasswordField[Invalid].backgroundPainter", p);
}
// example usage, toggling
// a new property (for simplicity implemented as clientProperty
// to toggle the invalid state
Action reset = new AbstractAction("reset") {
@Override
public void actionPerformed(ActionEvent e) {
boolean isInvalid = Boolean.TRUE.equals(field.getClientProperty("Invalid"));
if (isInvalid) {
field.putClientProperty("Invalid", null);
} else {
field.putClientProperty("Invalid", Boolean.TRUE);
}
field.repaint();
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句