我正在尝试将 css/html 表“附加”到 jtextpane。
当我这样做时:setText 像这样:
jtextpane.setText(css)
我得到了想要的结果[完美!]:
但是当我尝试像这样附加文本到jtextpane时:
int len = jtextpane.getDocument().getLength();
jtextpane.setCaretPosition(len);
jtextpane.replaceSelection(css);
我得到这样嵌入的 html 代码:
问:如何在 jtextPane 中附加表的结果(不是代码)?我假设我在 replaceSelection 上做错了什么?!提前致谢
编辑 - 附加信息:
- 要将所有文本信息附加到 jtextpane,我使用以下静态方法:
public static void appendToPane(JTextPane jtextpane, String userText, Color color) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color); aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Myriad Pro"); aset = sc.addAttribute(aset, StyleConstants.FontSize, 20); int len = jtextpane.getDocument().getLength(); jtextpane.setCaretPosition(len); jtextpane.setCharacterAttributes(aset, false); jtextpane.replaceSelection(userText); }
关于我拥有的 jtextpane 的实例化:
jtextpane.setContentType("文本/html");
原来的css字符串是这样的:
table.imagetable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.imagetable th { background:#b5cfd2 url('cell-blue.jpg'); border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } table.imagetable td { background:#dcddc0 url('cell-grey.jpg'); border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; }
最佳答案
您必须声明您在 JTextPane 中使用的文本类型
jtextPane.setContentType("text/html");
如果这不起作用,请尝试在您的文本中包含正确的 <html>
应该这样做。我前段时间遇到了同样的问题,我正在寻找具体的代码。
关于java - 如何在 jtextpane 中附加 css/html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18701792/