我有一个 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);
知道哪里出了问题吗?
最佳答案
Swing 组件将它们的外观委托(delegate)给 ComponentUI
对象。作为 Swing 的一部分,为每个组件定义了接口(interface):ButtonUI
,JButton
委托(delegate)给 LabelUI
,JLabel
, TextUI
for JTextPane
等
每个 Swing 外观都包含每个接口(interface)的实现。例如。 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);
关于java - 更改外观会更改 JTextPane 的颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228336/