Полагаю, насколько я догадался, вам нужно что-то кроме JTextArea, чтобы вы могли показывать разные вещи в разных цветах. Что ж, это можно сделать с помощью JTextPane или JEditorPane.
Здесь я приведу простой пример того, как предоставить JTextPane указанное сообщение для печати и цвет этого сообщения.
Вы можете изменить цвета для любого входного текста, чтобы он мог отображать различные цвета для каждого слова.
// This is in javax.swing.JTextPane;
JTextPane tPane = new JTextPane();
/* Method to put text in this textPane with colour of your choice
* For this you need these classes.
* import javax.swing.text.AttributeSet;
* import javax.swing.text.SimpleAttributeSet;
* import javax.swing.text.StyleConstants;
* import javax.swing.text.StyleContext;
*/
protected static void appendToPane(String msg, Color c,String f)
{
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, f);
int len = tPane.getDocument().getLength();
tPane.setCaretPosition(len);
tPane.setCharacterAttributes(aset, false);
tPane.replaceSelection(msg);
}
Так что, если вы так позвоните appendToPane("HI", Color.RED, "Lucida Console")
он будет отображать HI красным цветом с указанным шрифтом.
и если вы позвоните, скажите appendToPane(" THERE", Color.BLACK, "TIMES NEW ROMAN")
тогда там будет напечатано черным цветом в том же месте рядом с Привет.