JTextPane 每次我插入字符串时都会添加大空格
JTextPane adding large spaces every time i insertString
嘿,我正在制作一个聊天应用程序,最初使用简单的 JTextPane 作为基本的、支持颜色的聊天视图窗格。然后我想添加 html link 支持,通过添加 HTML 侦听器并将内容类型设置为 text/html 使它们可点击。可点击的 link 工作得很好,但现在每次我插入一个字符串时,聊天都会添加一个大的 space。这是我在下面使用的代码:
构造函数:
public JTextPaneTest() {
this.addHyperlinkListener(new LinkController());
this.setContentType("text/html");
this.setEditable(false);
}
这是我附加常规文本的方式:
public void append(Color c, String s) {
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, c);
StyledDocument doc = (StyledDocument)this.getDocument();
int len = getDocument().getLength();
try {
doc.insertString(len, s, sas);
} catch (BadLocationException e) {
e.printStackTrace();
}
setCaretPosition(len + s.length());
}
这是我如何插入links
public void addHyperlink(URL url, String text) {
try {
Document doc = this.getDocument();
SimpleAttributeSet hrefAttr = new SimpleAttributeSet();
hrefAttr.addAttribute(HTML.Attribute.HREF, url.toString());
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTML.Tag.A, hrefAttr);
StyleConstants.setUnderline(attrs, true);
StyleConstants.setForeground(attrs, Color.blue);
doc.insertString(doc.getLength(), text, attrs);
}
catch (BadLocationException e) {
e.printStackTrace(System.err);
}
}
无论出于何种原因将内容类型设置为基本文本,我都没有得到这个 space 问题。
这是它的一些照片:
http://i.stack.imgur.com/dpMBB.png
在图片中,插入了名称,然后是:,然后是其余的文字。
编辑:无论出于何种原因,JTextPane 都会自动将我的 InsertStrings 居中。
Edit2:是否可以删除插入的 HTML 字符串之间的边距?我已经连续几个小时尝试了所有方法,但就是找不到解决方案。我能想到的唯一可能的解决方案是每次插入字符串时通过 getText/setText 重新格式化文本以确保不添加边距..
与其插入带有属性的文本,不如尝试创建一个虚拟元素并将其替换为包含 link
的外部 HTML
SimpleAttributeSet a=new SimpleAttributeSet();
a.addAttribute("DUMMY_ATTRIBUTE_NAME","DUMMY_ATTRIBUTE_VALUE");
doc.setCharacterAttributes(start, text.length(), a, false);
Element elem=doc.getCharacterElement(start);
String html="<a href='"+text+"'>"+text+"</a>";
doc.setOuterHTML(elem, html);
Se 工作示例 here
我最终只使用了基本的 'text' 并使用组件进行点击检测。
为了解决我在使用 HTML 时遇到的这个问题,我只是使用了
editorKit.insertHTML(doc, doc.getLength(), "html code", 0, 0, null);
然后使用文档的 'insertString'
直接插入我的代码
嘿,我正在制作一个聊天应用程序,最初使用简单的 JTextPane 作为基本的、支持颜色的聊天视图窗格。然后我想添加 html link 支持,通过添加 HTML 侦听器并将内容类型设置为 text/html 使它们可点击。可点击的 link 工作得很好,但现在每次我插入一个字符串时,聊天都会添加一个大的 space。这是我在下面使用的代码:
构造函数:
public JTextPaneTest() {
this.addHyperlinkListener(new LinkController());
this.setContentType("text/html");
this.setEditable(false);
}
这是我附加常规文本的方式:
public void append(Color c, String s) {
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, c);
StyledDocument doc = (StyledDocument)this.getDocument();
int len = getDocument().getLength();
try {
doc.insertString(len, s, sas);
} catch (BadLocationException e) {
e.printStackTrace();
}
setCaretPosition(len + s.length());
}
这是我如何插入links
public void addHyperlink(URL url, String text) {
try {
Document doc = this.getDocument();
SimpleAttributeSet hrefAttr = new SimpleAttributeSet();
hrefAttr.addAttribute(HTML.Attribute.HREF, url.toString());
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTML.Tag.A, hrefAttr);
StyleConstants.setUnderline(attrs, true);
StyleConstants.setForeground(attrs, Color.blue);
doc.insertString(doc.getLength(), text, attrs);
}
catch (BadLocationException e) {
e.printStackTrace(System.err);
}
}
无论出于何种原因将内容类型设置为基本文本,我都没有得到这个 space 问题。
这是它的一些照片: http://i.stack.imgur.com/dpMBB.png
在图片中,插入了名称,然后是:,然后是其余的文字。
编辑:无论出于何种原因,JTextPane 都会自动将我的 InsertStrings 居中。
Edit2:是否可以删除插入的 HTML 字符串之间的边距?我已经连续几个小时尝试了所有方法,但就是找不到解决方案。我能想到的唯一可能的解决方案是每次插入字符串时通过 getText/setText 重新格式化文本以确保不添加边距..
与其插入带有属性的文本,不如尝试创建一个虚拟元素并将其替换为包含 link
的外部 HTML SimpleAttributeSet a=new SimpleAttributeSet();
a.addAttribute("DUMMY_ATTRIBUTE_NAME","DUMMY_ATTRIBUTE_VALUE");
doc.setCharacterAttributes(start, text.length(), a, false);
Element elem=doc.getCharacterElement(start);
String html="<a href='"+text+"'>"+text+"</a>";
doc.setOuterHTML(elem, html);
Se 工作示例 here
我最终只使用了基本的 'text' 并使用组件进行点击检测。
为了解决我在使用 HTML 时遇到的这个问题,我只是使用了
editorKit.insertHTML(doc, doc.getLength(), "html code", 0, 0, null);
然后使用文档的 'insertString'
直接插入我的代码