2

テキストエリアの特定の行に色を設定したい。私がこれまでに見つけたのは、次のとおりです

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

しかし、これは機能していません。私は何を間違っていますか?

編集:OK、私は物事を試していて、使ってみました

final int end = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1);
document.insertString(end, "someText", awayAttributeSet);

追加してスタイルを変更する代わりにテキストを追加しますが、役に立ちません。

4

2 に答える 2

9

おそらく、選択したフォント、色などからドキュメント全体のスタイルを設定するため、JTextArea をそれほど詳細にスタイル設定できるかどうかはわかりません。

編集: javadoc から

JTextArea は、プレーンテキストを表示する複数行の領域です。

(強調を加えます。)

JTextPane に移動できる場合は、書式設定がレンダリングされます。

于 2010-05-14T22:30:29.297 に答える
0

たとえば PlainDocument を拡張するカスタム Swing ドキュメントを作成し、トークンの描画を担当するカスタム HighlightedView を作成します。

于 2010-05-16T00:59:53.857 に答える