下面这个是我在网上找的一个JTextPane例子,我想知道的是为什么在StyleConstants.setForeground(attrSet, col)方法之后,然后再调用document.insertString(doc.getLength(), str, attrSet)方法就可以把颜色显示出来???是不是调用了Graphics之类的类??  我看了java源码,没有找到相关的地方,希望哪位高手告诉下谢谢
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;public class Test {
JFrame frame, frame2; JTextPane textPane, textPane2; File file; Icon image; public Test() {
frame = new JFrame("JTextPane");
frame2 = new JFrame("提取的内容");
textPane = new JTextPane();
textPane2 = new JTextPane();
file = new File("icon.gif");
image = new ImageIcon(file.getAbsoluteFile().toString());
} public void insert(String str, AttributeSet attrSet) {
Document doc = textPane.getDocument();
str = "\n" + str;
try {
doc.insertString(doc.getLength(), str, attrSet);
} catch (BadLocationException e) {
System.out.println("BadLocationException:   " + e);
}
} public void setDocs(String str, Color col, boolean bold, int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
// 颜色
if (bold == true) {
StyleConstants.setBold(attrSet, true);
}// 字体类型
StyleConstants.setFontSize(attrSet, fontSize);
// 字体大小
insert(str, attrSet);
} public void gui() {
textPane.insertIcon(image);
setDocs("第一行的文字", Color.red, false, 20);
setDocs("第二行的文字", Color.BLACK, true, 25);
setDocs("第三行的文字", Color.BLUE, false, 20);
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame2.getContentPane().add(textPane2, BorderLayout.CENTER);
frame.setSize(200, 300);
frame.setVisible(true);
frame2.setLocation(200, 0);
frame2.setSize(200, 300);
frame2.setVisible(true);
} public static void main(String[] args) {
Test test = new Test();
test.gui();
StyledDocument docs = test.textPane.getStyledDocument();// 取得textPane中的StyledDocument类文档
test.textPane2.setStyledDocument(docs);// 将StyledDocument类文档传给textPane2

}
}