A commonly asked question is how to display text in multiple colors or styles within a JTextArea component. The short answer is: you can't. Both the JTextField and JTextArea components are designed to display text in a single style, color, font. When you change the characteristics of the text, you are changing all text in the component. You can't change the characteristics of only the selected text, or just the piece you want changed.     Just because the JTextArea component can't display its content in multiple colors or styles, doesn't mean there isn't support for displaying text in multiple styles. It just isn't done with the JTextArea. You need to use the JTextPane. Knowing that it is the JTextPane and not the JTextArea will lead you to the actual "how" answer to the original question.     Within the JTextPane, text that you add to the component is associated with an AttributeSet. The AttributeSet contains a set of key-value pairs for the various display attributes of the text. These pairs can answer questions like "what's the current font?", "what's the background color?", and "with what alignment should I display the current paragraph?" By setting this AttributeSet before adding the text, you can change the way the added text is displayed. You can also change the AttributeSet for the currently selected text. 

解决方案 »

  1.   

    去sun下一份java tutorial
    看看2D部分.
    Trail: 2D Graphics ->
    Lesson: Displaying Graphics with Graphics2D  ->
    Stroking and Filling Graphics Primitives 我们应学会看文档及tutorial
      

  2.   

    用Graphics2D类的方法设置一下就可以了,
    如:Graphics2D g2d=(Graphics2D)g;
    g2d.setStroke(new BasicStroke(6.0f);
    ……