【问题标题】:How to add text to a textArea instead of replacing it如何将文本添加到 textArea 而不是替换它
【发布时间】:2012-08-26 08:08:47
【问题描述】:

如何向JTextArea 添加文本而不是全部替换?

我知道setText(String),但除此之外我有点迷茫。

【问题讨论】:

标签: java string swing jtextarea


【解决方案1】:

你可以像这样使用the append method

textArea.append(additionalText);

【讨论】:

  • text 部分是什么?
【解决方案2】:
void append(JTextArea area, String newText){
        area.setText(area.getText() + newText)
}

【讨论】:

  • -1 用于重新发明轮子(api doc 是了解可用轮子的绝佳资源 :-)
  • area.setText(area.getText() + newText) 正是我想要的,感谢 Pawel。
【解决方案3】:

要在任何位置插入字符串,您可以使用组件的文档。

public static void main(String[] args) throws BadLocationException {
    JTextField f = new JTextField("foo bar");
    int offset = 7;
    String str = " baz";
    f.getDocument().insertString(offset, str, SimpleAttributeSet.EMPTY);
    System.out.println(f.getText());
}

【讨论】:

    猜你喜欢
    • 2015-02-05
    • 2022-11-18
    • 2016-03-21
    • 2013-08-26
    • 2022-01-12
    • 1970-01-01
    • 2016-04-29
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多