elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Cómo usar JTextPane con fuentes y tamaños diferentes
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Cómo usar JTextPane con fuentes y tamaños diferentes  (Leído 2,818 veces)
JenselG

Desconectado Desconectado

Mensajes: 18


Ver Perfil
Cómo usar JTextPane con fuentes y tamaños diferentes
« 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,


En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Cómo usar JTextPane con fuentes y tamaños diferentes
« Respuesta #1 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.  




« Última modificación: 11 Octubre 2018, 09:54 am por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
limitar numero de lineas que caben en un JTextPane, como si fuese una shell
Java
kinos 6 7,613 Último mensaje 12 Agosto 2009, 02:05 am
por kinos
Usar dos memorias RAM de diferentes marcas.
Hardware
Meta 2 12,061 Último mensaje 5 Septiembre 2012, 15:30 pm
por Meta
[pregunta]como separo diferentes threats en diferentes ventanas en windows « 1 2 »
Programación C/C++
daryo 10 5,053 Último mensaje 4 Octubre 2013, 23:10 pm
por daryo
Como justificar párrafo de texto de jTextPane o de jTextArea
Java
soy_nicanor 1 9,625 Último mensaje 23 Enero 2015, 19:06 pm
por Usuario Invitado
¿como usar un mismo metodo con diferentes jtable?
Java
SoyBolita 1 1,839 Último mensaje 20 Febrero 2016, 19:26 pm
por 0xFer
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines