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

查看:156
本文介绍了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:"

为什么会发生这种情况???

Why did this happen???

推荐答案

我怀疑那是建议的附加文本的方法。这意味着每次更改某些文本时,都需要重新整理整个文档。人们可能这样做的原因是因为不了解如何使用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\n", null );
    doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
}
catch(Exception e) { System.out.println(e); }

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

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