16

誰かが簡単なログで私を助けることができますか、私は選択された色(緑のOK、赤の失敗)でJTextPaneログメッセージの最初の行に追加する必要があります。これを達成する方法は?

4

3 に答える 3

36

これにより、「BLAH BLEG」が 2 色で印刷されます。

public class Main {
    public static void main(String[] args) {
        JTextPane textPane = new JTextPane();
        StyledDocument doc = textPane.getStyledDocument();

        Style style = textPane.addStyle("I'm a Style", null);
        StyleConstants.setForeground(style, Color.red);

        try { doc.insertString(doc.getLength(), "BLAH ",style); }
        catch (BadLocationException e){}

        StyleConstants.setForeground(style, Color.blue);

        try { doc.insertString(doc.getLength(), "BLEH",style); }
        catch (BadLocationException e){}

        JFrame frame = new JFrame("Test");
        frame.getContentPane().add(textPane);
        frame.pack();
        frame.setVisible(true);
    }
}

こちらをご覧ください:スタイルのチュートリアル

色を動的に変更する方法の優れた例については、「テキスト ペインの使用例」というラベルの付いたセクションを確認してください。

于 2011-05-20T07:30:33.943 に答える
10

JTextPaneの場合、http://www.java2s.com/Code/Java/Swing-JFC/TextPane.htmStyledDocumentを実装できます。

于 2011-05-20T07:12:09.963 に答える
0

そのためにHTMLを使用してから行うことができます

textPane.setContentType("text/html");
于 2015-02-21T20:24:43.313 に答える