JTextPane 附加一个新字符串 [英] JTextPane appending a new string

查看:26
本文介绍了JTextPane 附加一个新字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每篇文章中,都会回答如何将字符串附加到 JEditorPane?"这个问题.就像

In an every article the answer to a question "How to append a string to a JEditorPane?" is something like

jep.setText(jep.getText + "new string");

我已经试过了:

jep.setText("<b>Termination time : </b>" + 
                        CriterionFunction.estimateIndividual_top(individual) + " </br>");
jep.setText(jep.getText() + "Processes' distribution: </br>");

结果我得到了终止时间:1000"而没有进程分布:"

And as a result I got "Termination time : 1000" without "Processes' distribution:"

为什么会这样???

推荐答案

我怀疑这是添加文本的推荐方法.这意味着每次更改某些文本时都需要重新解析整个文档.人们可能这样做的原因是因为他们不了解如何使用 JEditorPane.这包括我.

I doubt that is the recommended approach for appending text. This means every time you change some text you need to reparse the entire document. The reason people may do this is because the don't understand how to use a JEditorPane. That includes me.

我更喜欢使用 JTextPane 然后使用属性.一个简单的例子可能是这样的:

I much prefer using a JTextPane and then using attributes. A simple example might be something like:

JTextPane textPane = new JTextPane();
textPane.setText( "original text" );
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);

//  Add some text

try
{
    doc.insertString(0, "Start of text
", null );
    doc.insertString(doc.getLength(), "
End of text", keyWord );
}
catch(Exception e) { System.out.println(e); }

这篇关于JTextPane 附加一个新字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆