import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class ColoredText extends JTextPane {

    ColoredText() {
	setEditable(false);
	setBackground(Color.gray);
    }

    void add(Color col,String txt) {
	SimpleAttributeSet attr = new SimpleAttributeSet();
	StyleConstants.setForeground(attr,col);
	Document doc=getDocument();
	try{
	    doc.insertString(doc.getLength(),txt,attr);
	} catch(BadLocationException e) {
	    e.printStackTrace();
	    System.exit(-1);
	}
    }

    public static void main(String args[]) {

	JFrame frame = new JFrame("ColorDemo");

	ColoredText text = new ColoredText();
	frame.setSize(new Dimension(400,500));
	frame.getContentPane().add(text);

	text.add(Color.red,"Rot\n");
	text.add(Color.green,"Grün\n");
	text.add(Color.yellow,"Gelb\n");
	text.add(Color.blue,"Blau\n");

	frame.setVisible(true);
    }

}
