我有JTextPane
有不同的背景和前景色。现在当L& F改为Nimbus L& F时,我JTextPane
的颜色发生了变化。怎么样?只有这个班级有这样的问题。其他人工作得很好。问题是什么?
这就是我改变L& F的方式:
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
另一方面,在设置L& F之前,颜色变化很好。 或者这些陈述运作良好:
jtp.setBackground(Color.BLACK);
jtp.setForeground(Color.WHITE);
知道出了什么问题吗?
答案 0 :(得分:4)
Swing组件将其外观委托给ComponentUI
个对象。作为Swing的一部分,为每个组件定义了接口:ButtonUI
委托给JButton
,LabelUI
代表JLabel
,TextUI
代表JTextPane
等等。
每个Swing外观都包含每个接口的实现。例如。 MetalButtonUI
,MetalLabelUI
等等绘制了该组件,但外观和感觉都是如此。
当你致电UIManager.setLookAndFeel
时,它会在这组实施中进行交换。
非常聪明,但令人讨厌的是,每个外观都不必遵守任何前景/背景/边框等设置。
幸运的是,Nimbus将其所有颜色定义为UIManager键。
所以,你可以做这种事情来覆盖它的默认颜色:
UIManager.put("nimbusBase", Color.BLACK);
请在此处查看完整列表:
<强>更新强>
虽然说,但看起来Nimbus看起来并不好看! 有些人用这个有一些运气压倒了Nimbus的颜色:
Color bgColor = new Color("99999");
UIDefaults defaults = new UIDefaults();
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
jeditorpane.putClientProperty("Nimbus.Overrides", defaults);
jeditorpane.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
jeditorpane.setBackground(bgColor);
答案 1 :(得分:3)
这是Spoon先生更新的略微更新。实际上是代码
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
错误,因为put调用的第二个参数应该是实现Painter接口的对象。
正确的代码序列(选择了Nimbus LAF)是
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("TextPane[Enabled].backgroundPainter",
new javax.swing.plaf.nimbus.AbstractRegionPainter() {
@Override
protected AbstractRegionPainter.PaintContext getPaintContext() {
return new AbstractRegionPainter.PaintContext(null, null, false);
}
@Override
protected void doPaint(Graphics2D g, JComponent c,
int width, int height, Object[] extendedCacheKeys) {
g.setColor(bgColor);
g.fillRect(0, 0, width, height);
}
});
jtxtPane.putClientProperty("Nimbus.Overrides", defaults);
jtxtPane.putClientProperty("Nimbus.Overrides.InheritDefaults", false);