텍스트 영역의 특정 줄에 색상을 설정하고 싶습니다. 내가 지금까지 발견 한 것은, 다음JTextArea에서 부분 텍스트 색을 어떻게 설정할 수 있습니까?
// Declarations
private final DefaultStyledDocument document;
private final MutableAttributeSet homeAttributeSet;
private final MutableAttributeSet awayAttributeSet;
// Usage in the form constructor
jTextAreaLog.setDocument(document);
homeAttributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(homeAttributeSet, Color.blue);
StyleConstants.setItalic(homeAttributeSet, true);
awayAttributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(awayAttributeSet, Color.red);
// Setting the style of the last line
final int start = jTextAreaLog.getLineStartOffset(jTextAreaLog.getLineCount() - 2);
final int length = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1) - start;
document.setCharacterAttributes(start, length, awayAttributeSet, true);
입니다하지만이 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
편집 : 좋아, 내가 일을하려고했는데 내가 다시 스타일에 다음 추가하는 대신 텍스트를 추가
final int end = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1);
document.insertString(end, "someText", awayAttributeSet);
를 사용했지만, 아무 소용.
JTextArea는 스타일이 지정된 텍스트를 지원하지 않습니다. JTextPane을 사용하십시오. – camickr