刚刚尝试为 JTextPane 中的文本着色 - 但问题是文本和下划线不能有不同的颜色。我应该怎么做或者这是否可能?下面的示例以红色打印所有文本和下划线。
JTextPane pane = new JTextPane();
StyleContext context = new StyleContext();
Style style = pane.addStyle("Black", null);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
StyleConstants.setForeground(style, Color.BLACK);
StyledDocument document = pane.getStyledDocument();
style = pane.addStyle("Red Underline", style);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setUnderline(style, true);
pane.getDocument().insertString(0, "Test String", style);
这是我找到的解决方案...
使用 AttributeSet 以不同颜色为 StyleConstant 下划线 https://stackoverflow.com/questions/9502654/underline-styleconstant-in-a-different-colour-with-attributeset
这是示例代码的链接
http://java-sl.com/tip_colored_strikethrough.html http://java-sl.com/tip_colored_strikethrough.html
只需更改以下行即可解决它...
int y = a.getBounds().y + a.getBounds().height + 2 ;
效果很好
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)