下面是顯示紅色文字的程式碼例子,其他的設定類似,是由AttributeSet設定的。
package baidu.zhidao;
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.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class TextPane extends JFrame
{
JTextPane jtp = null;
DefaultStyledDocument doc = null;
SimpleAttributeSet attrset = null;
public TextPane()
super("JTextPane model");
doc = new DefaultStyledDocument();
jtp = new JTextPane(doc);
attrset = new SimpleAttributeSet();
StyleConstants.setForeground(attrset, Color.red);
try
doc.insertString(doc.getLength(), "你好,我是jink2005。\n", attrset);
}
catch (BadLocationException e)
this.getContentPane().add(new JScrollPane(jtp));
this.pack();
this.setVisible(true);
public static void main(String[] args)
new TextPane();
下面是顯示紅色文字的程式碼例子,其他的設定類似,是由AttributeSet設定的。
package baidu.zhidao;
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.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class TextPane extends JFrame
{
JTextPane jtp = null;
DefaultStyledDocument doc = null;
SimpleAttributeSet attrset = null;
public TextPane()
{
super("JTextPane model");
doc = new DefaultStyledDocument();
jtp = new JTextPane(doc);
attrset = new SimpleAttributeSet();
StyleConstants.setForeground(attrset, Color.red);
try
{
doc.insertString(doc.getLength(), "你好,我是jink2005。\n", attrset);
}
catch (BadLocationException e)
{
}
this.getContentPane().add(new JScrollPane(jtp));
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
new TextPane();
}
}