1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
import java.awt.Color;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
DefaultStyledDocument doc = new DefaultStyledDocument();
log.setDocument(doc);
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setFontFamily(def, "SansSerif");
Style regular = log.addStyle("regular", def);
Style s = log.addStyle("red", regular);
StyleConstants.setForeground(s, Color.RED);
s = log.addStyle("green", regular);
StyleConstants.setForeground(s, Color.GREEN);
s = log.addStyle("blue", regular);
StyleConstants.setForeground(s, Color.BLUE);
s = log.addStyle("black", regular);
StyleConstants.setForeground(s, Color.BLACK);
try {
doc.insertString(doc.getLength(), "ecrit en rouge\n", log.getStyle("red"));
doc.insertString(doc.getLength(), "ecrit en vert\n", log.getStyle("green"));
doc.insertString(doc.getLength(), "ecrit en bleu\n", log.getStyle("blue"));
} catch (BadLocationException ex) {
ex.printStackTrace();
} |
Partager