[Top ページ] [他のサンプル]

TestBoxAttributeSampleApplet.java


import java.awt.BorderLayout;
import java.awt.Color;
 
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
 
 
public class TestBoxAttributeSampleApplet extends JApplet {
	private JTextPane textPane_;
	
	public TestBoxAttributeSampleApplet() {
		textPane_ = new JTextPane();
		
		setLayout(new BorderLayout());
		add(new JScrollPane(textPane_), BorderLayout.CENTER);
		
		insertSampleText(textPane_.getDocument());
	}
	
	// 色つきのサンプルテキストを挿入
	private void insertSampleText(Document doc) {
		// ----------------------------------------------------------------------------
		// AttributeSet の準備
		// ----------------------------------------------------------------------------
		
		// 青い文字用の AttributeSet
		SimpleAttributeSet blueTextAttribSet = new SimpleAttributeSet();
		blueTextAttribSet.addAttribute(StyleConstants.Foreground, Color.BLUE);
		
		// 赤くて太字の文字用の AttributeSet
		SimpleAttributeSet redAndBoldTextAttribSet = new SimpleAttributeSet();
		redAndBoldTextAttribSet.addAttribute(StyleConstants.Foreground, Color.RED);
		redAndBoldTextAttribSet.addAttribute(StyleConstants.Bold, true);
		
		// ----------------------------------------------------------------------------
		// 挿入処理
		// ----------------------------------------------------------------------------
		try {
			doc.insertString(doc.getLength(), "BlueText ", blueTextAttribSet);
			doc.insertString(doc.getLength(), "RedAndBold\n", redAndBoldTextAttribSet);
			doc.insertString(doc.getLength(), "NormalText", null);
		} catch(Exception e) {}
	}
}