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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class Test extends JFrame {
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextPane jTextPane = new JTextPane();
jTextPane.setForeground(Color.GREEN.darker());
Style defaut = jTextPane.getStyle("default");
Style style1 = jTextPane.addStyle("style1", defaut);
StyleConstants.setFontFamily(style1, "Comic sans MS");
StyleConstants.setFontSize(style1, 14);
StyleConstants.setForeground(style1, Color.BLUE);
Style style2 = jTextPane.addStyle("style2", style1);
StyleConstants.setForeground(style2, Color.RED);
StyleConstants.setFontSize(style2, 25);
String s1 = "Bonjour\r\n";
String s2 = "Bonjour\r\n";
String s3 = "Bonjour\r\n";
StyledDocument sDoc = (StyledDocument) jTextPane.getDocument();
try {
int pos = 0;
sDoc.insertString(pos, s1, defaut);
pos += s1.length();
sDoc.insertString(pos, s2, style1);
pos += s2.length();
sDoc.insertString(pos, s3, style2);
} catch (BadLocationException e) {
//
}
setContentPane(new JScrollPane(jTextPane));
}
public static void main(String[] args) {
Test frame = new Test();
frame.pack();
frame.setVisible(true);
}
} |
Partager