Foro de elhacker.net

Programación => Java => Mensaje iniciado por: JenselG en 23 Septiembre 2018, 02:48 am



Título: Cómo usar JTextPane con fuentes y tamaños diferentes
Publicado por: JenselG en 23 Septiembre 2018, 02:48 am
Hola a todos, mi problema es que estoy creando una ventana de ayuda y necesito mostrar la ayuda con tamaños y fuentes diferentes en un JTextPane. Hasta ahora no he encontrado ayuda que me acerque a mi objetivo.

¿Cómo puedo hacerlo? ¿Podrian darme una estructura o un ejemplo? Gracias,


Título: Re: Cómo usar JTextPane con fuentes y tamaños diferentes
Publicado por: rub'n en 24 Septiembre 2018, 09:02 am
Hasta ahora no he encontrado ayuda que me acerque a mi objetivo

Para la próxima en lo posible coloca código hecho >:D


https://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java

Código
  1. import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.*;
  4.  
  5. public class TestForo  extends JPanel {
  6.  
  7.    private static final String newline = "\n";
  8.  
  9.    public TestForo() {
  10.        setLayout(new BorderLayout());
  11.        setPreferredSize(new Dimension(400,300));
  12.        JScrollPane scrollPane = new JScrollPane(createTextPane());
  13.        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  14.        scrollPane.setPreferredSize(new Dimension(10,10));
  15.        scrollPane.setMinimumSize(new Dimension(10 , 10));
  16.  
  17.        add(scrollPane);
  18.  
  19.    }
  20.  
  21.    private JTextPane createTextPane() {
  22.        String[] initString =
  23.                { "JenselGe hola soy java, espero que ejecutes esto XD, ", //regular
  24.                        "another ",                                   //italic
  25.                        "styled ",                                    //bold
  26.                        "text ",                                      //small
  27.                        "component, ",                                //large
  28.                        "which supports embedded components..." + newline,//regular
  29.                        "y no pondre lloronas XD " + newline,          //button
  30.                        "...and embedded icons..." + newline,         //regular
  31.  
  32.                        newline + "JTextPane is a subclass of JEditorPane that " +
  33.                                "uses a StyledEditorKit and StyledDocument, and provides " +
  34.                                "cover methods for interacting with those objects."
  35.                };
  36.  
  37.        String[] initStyles = { "regular", "italic", "bold", "small", "large", "regular", "regular","regular","bold"};
  38.  
  39.        JTextPane textPane = new JTextPane();
  40.        StyledDocument doc = textPane.getStyledDocument();
  41.        addStylesToDocument(doc);
  42.  
  43.        try {
  44.            for (int i=0; i < initString.length; i++) {
  45.                doc.insertString(doc.getLength(), initString[i], doc.getStyle(initStyles[i]));
  46.            }
  47.        } catch (BadLocationException ble) {
  48.            System.err.println("Couldn't insert initial text into text pane.");
  49.        }
  50.  
  51.        return textPane;
  52.    }
  53.  
  54.    protected void addStylesToDocument(StyledDocument doc) {
  55.        //Initialize some styles.
  56.        Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  57.  
  58.        Style regular = doc.addStyle("regular", def);
  59.        StyleConstants.setFontFamily(def, Font.SANS_SERIF);
  60.  
  61.        Style s = doc.addStyle("italic", regular);
  62.        StyleConstants.setItalic(s, true);
  63.  
  64.        s = doc.addStyle("bold", regular);
  65.        StyleConstants.setBold(s, true);
  66.  
  67.        s = doc.addStyle("small", regular);
  68.        StyleConstants.setFontSize(s, 10);
  69.  
  70.        s = doc.addStyle("large", regular);
  71.        StyleConstants.setFontSize(s, 16);
  72.  
  73.        s = doc.addStyle("bold" , regular);
  74.        StyleConstants.setFontSize(s , 19);
  75.  
  76.  
  77.    }
  78.  
  79.    public static void main(String ...agrea ) {
  80.        new Thread(() -> {
  81.           JFrame jFrame = new JFrame("TextEditable Ejemplo");
  82.           jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83.           jFrame.add(new TestForo());
  84.           jFrame.pack();
  85.           jFrame.setLocationRelativeTo(null);
  86.           jFrame.setVisible(true);
  87.        }).start();
  88.    }
  89. }
  90.  

(http://1.bp.blogspot.com/-dDarzsGaHQg/W78Bp2Xkr4I/AAAAAAAADG4/JZO1wk17rhM-4_oe_mkv5bTNGwIfS5SgQCK4BGAYYCw/s1600/JtextPane.jpg)