JTextPane 追加新字符串

发布于 2024-09-30 02:17:20 字数 432 浏览 4 评论 0原文

每篇文章都会回答“如何将字符串附加到 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”而没有“进程分布:”

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

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");

I have tried this:

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

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

Why did this happen???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹雨成花 2024-10-07 02:17:20

我怀疑这是附加文本的推荐方法。这意味着每次更改某些文本时,您都需要重新解析整个文档。人们这样做的原因可能是因为他们不了解如何使用 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); }

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.

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); }
临走之时 2024-10-07 02:17:20

JEditorPane 就像 JTextPane 一样,有一个可用于插入字符串的 Document

将文本附加到 JEditorPane 中所需执行的操作是以下代码片段:

JEditorPane pane = new JEditorPane();
/* ... Other stuff ... */
public void append(String s) {
   try {
      Document doc = pane.getDocument();
      doc.insertString(doc.getLength(), s, null);
   } catch(BadLocationException exc) {
      exc.printStackTrace();
   }
}

我对此进行了测试,它对我来说效果很好。 doc.getLength() 是您要插入字符串的位置,显然,使用此行您会将其添加到文本的末尾。

A JEditorPane, just a like a JTextPane has a Document that you can use for inserting strings.

What you'll want to do to append text into a JEditorPane is this snippet:

JEditorPane pane = new JEditorPane();
/* ... Other stuff ... */
public void append(String s) {
   try {
      Document doc = pane.getDocument();
      doc.insertString(doc.getLength(), s, null);
   } catch(BadLocationException exc) {
      exc.printStackTrace();
   }
}

I tested this and it worked fine for me. The doc.getLength() is where you want to insert the string, obviously with this line you would be adding it to the end of the text.

葬花如无物 2024-10-07 02:17:20

setText 是设置文本窗格中的所有文本。使用 StyledDocument 接口进行追加,删除等文本。

txtPane.getStyledDocument().insertString(
  offsetWhereYouWant, "text you want", attributesYouHope);

setText is to set all text in a textpane. Use the StyledDocument interface to append, remove, ans so on text.

txtPane.getStyledDocument().insertString(
  offsetWhereYouWant, "text you want", attributesYouHope);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文