因此,我创建了自己的文本窗格类(扩展 JTextPane),并使用下面的方法向其中添加文本。但是,窗格需要可编辑才能添加文本,但这也允许用户编辑窗格中的内容。
谁能告诉我如何在不让用户操作的情况下将文本添加到窗格中?
public void appendColor(Color c, String s) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength();
setCaretPosition(len);
setCharacterAttributes(aset, false);
replaceSelection(s);
setCaretPosition(getDocument().getLength());
}