2011-05-20 19 views

Respuesta

30

Esto imprimirá "BLA BLEG" en dos colores diferentes.

public class Main { 
    public static void main(String[] args) { 
     JTextPane textPane = new JTextPane(); 
     StyledDocument doc = textPane.getStyledDocument(); 

     Style style = textPane.addStyle("I'm a Style", null); 
     StyleConstants.setForeground(style, Color.red); 

     try { doc.insertString(doc.getLength(), "BLAH ",style); } 
     catch (BadLocationException e){} 

     StyleConstants.setForeground(style, Color.blue); 

     try { doc.insertString(doc.getLength(), "BLEH",style); } 
     catch (BadLocationException e){} 

     JFrame frame = new JFrame("Test"); 
     frame.getContentPane().add(textPane); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

vistazo aquí: Style Tutorial

y comprobar la sección titulada: Un ejemplo de uso de un panel de texto para un gran ejemplo de cómo cambiar dinámicamente los colores.

+0

+1 cosas buenas. Me gusta el ejemplo. – Boro

+0

funcionó para mí, gracias. –

0

Usted puede usar HTML para eso y luego hacer

textPane.setContentType("HTML/plain"); 
Cuestiones relacionadas