如何在JavaSwing中为样式化文档设置全局样式
我有一个StyledDocument,我正在添加一些样式。我希望有一些默认(全局)样式,适用于我创建的所有样式。例如,所有样式的全局背景,这样我就不必为每个样式指定如何在JavaSwing中为样式化文档设置全局样式,java,swing,styles,styleddocument,Java,Swing,Styles,Styleddocument,我有一个StyledDocument,我正在添加一些样式。我希望有一些默认(全局)样式,适用于我创建的所有样式。例如,所有样式的全局背景,这样我就不必为每个样式指定背景 以下是我试图实现的目标 public void setUpStyles() { parentMSGStyle = historyPane.addStyle("parentmsgstyle", null); userNameStyle = historyPane.addStyle("user
背景
以下是我试图实现的目标
public void setUpStyles() {
parentMSGStyle = historyPane.addStyle("parentmsgstyle", null);
userNameStyle = historyPane.addStyle("usernamestyle", parentMSGStyle);
StyleConstants.setBackground(parentMSGStyle, Color.GRAY);
StyleConstants.setForeground(userNameStyle, Color.BLUE);
}
现在还没有。只有蓝色款式有效,但“灰色”款式无效。
我对时尚文件不熟悉。请给我指出正确的方向。看到这一点后,我自己也试了一下,也弄糊涂了。根据childStyle=addStyle(String nm,parentStyle)的定义,
应显示以下行为:
将新样式添加到逻辑样式层次结构中。样式属性
自下而上解析,以便在子对象中指定的属性
重写父级中指定的属性
只要我们没有为子样式设置背景色,它就不应该覆盖父样式的属性
StyleConstants.setBackground(parentMSGStyle, Color.GREY);
StyleConstants.setForeground(userNameStyle, Color.BLUE);
如果我们现在尝试使用document.getBackground(usernamestyle)
打印文档的背景色,它应该打印灰色
,并且它会这样做。
因此,不知为什么,这不仅仅是在屏幕上。但令人惊讶的是,相反的结果会起作用。也就是说,将Forground
设置为parentStyle,将Background设置为childStyle
StyleConstants.setForeground(parentMSGStyle, Color.BLUE);
StyleConstants.setBackground(userNameStyle, Color.GREY);
别告诉我这不是一个解决办法。:)