3

私がやろうとしていることが可能かどうかはわかりません。

次のように宣言された書式付きテキストを追加するコンソールがあります。

private final JTextPane statusText = new JTextPane();

次のようなスタイル付きドキュメントへの参照を取得しました。

private StyledDocument statusDocument = statusText.getStyledDocument();

いくつかの属性を定義しました:

private final SimpleAttributeSet gray;
private final SimpleAttributeSet black;
private final SimpleAttributeSet red;

およびヘルパー メソッド:

private void appendStatusText(String text, SimpleAttributeSet attribute) {
        final String finalText = text;
        final SimpleAttributeSet finalAttribute = attribute;
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    statusDocument.insertString(statusDocument.getLength(), finalText, finalAttribute);
                } catch (BadLocationException e) {
                    log.error("Cannot add " + finalText, e);
                }
            }
        });
    }

属性 (灰色、赤、黒) の 1 つといくつかのテキストでappendStatusTextを使用したいのですが、表示されているのはすべて灰色で、多色を期待しています。

助けてください。

PS:ここの質問からコードを取得しました

4

2 に答える 2

4

initDocument()方法は、TextComponentDemoこのようなドキュメントを作成するための 1 つのアプローチを示しています。このは、チュートリアル記事How to Use Editor Panes and Text PanesExamples That Use Text Panes and Editor Panesに含まれています。

于 2012-04-17T19:28:31.270 に答える
0

SimpleAttributeSet を定義してから、次のように必要な属性を追加する必要があります。

private SimpleAttributeSet red = new SimpleAttributeSet();      
red.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.red);
于 2012-07-10T16:09:21.217 に答える