【发布时间】:2012-08-17 19:41:20
【问题描述】:
刚刚尝试为 JTextPane 中的文本着色 - 但问题是文本和下划线不能有不同的颜色。我应该怎么做,或者这甚至可能吗?下面的示例以 RED 打印所有文本和下划线。
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);
【问题讨论】:
-
+1 尽管 Style API 似乎是可扩展的,但我找不到任何文档。
-
在这里找到答案...stackoverflow.com/questions/9502654/…
标签: java swing highlighting jtextpane