在每篇文章中都回答了“如何将字符串附加到 JEditorPane?”这个问题。有点像
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”而没有“进程的分布:”
为什么会这样???
最佳答案
我怀疑这是附加文本的推荐方法。这意味着每次更改某些文本时,都需要重新解析整个文档。人们可能这样做的原因是不了解如何使用 JEditorPane。这包括我。
我更喜欢使用 JTextPane,然后使用属性。一个简单的例子可能是这样的:
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); }
关于java - JTextPane 附加一个新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4059198/