2012-09-22 14 views
9
editorPane.setContentType("text/html");  
editorPane.setFont(new Font("Segoe UI", 0, 14)); 
editorPane.setText("Hello World"); 

Esto no cambia la fuente del texto. Necesito saber cómo configurar la fuente predeterminada para JEditorPane con HTML Editor Kit.Configuración de fuente predeterminada en JEditorPane

Editar:

enter image description here

+4

Por favor, publique su código en formato de texto y no una imagen de él, porque cualquiera que quiera probarlo tiene que escribirlo. Esto no es escuela :) –

+0

Más sobre cómo tomar una [captura de pantalla] (http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a- enviar). – trashgod

Respuesta

1

He comprobado su código, no debería haber ningún problema. ¿Has probado otras fuentes? Por favor, pruebe la fuente "Segoe Script" y vea si cambia.

Editar: He intentado el código a continuación, funciona bien para mí. ¿Estás seguro de que el código que has publicado es exactamente el mismo que has implementado?

editorPane.setContentType("text/html"); 
    editorPane.setFont(new Font("Segoe Script", 0, 14)); 
    editorPane.setText("it works!"); 

Edit2: cambiar su método principal de la siguiente manera. Establece el Nimbus LookAndFeel. Todavía no he revisado otros LookAndFeels.

public static void main(String[] args) 
{ 
    try 
    { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
     { 
      if ("Nimbus".equals(info.getName())) 
      { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) 
    { 
     java.util.logging.Logger.getLogger(EditorPaneDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 

    java.awt.EventQueue.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      new EditorPaneDemo(); 
     } 
    }); 
} 
+1

el código funciona, pero la fuente no se actualiza. – Sanjeev

+0

como mencioné en mi respuesta editada, asegúrese de haber implementado el código exacto –

+0

observe la imagen cargada, ya que el texto no es del script segoe de fuente. – Sanjeev

1

intento por debajo

editorPane.setFont(new Font("Segoe UI", Font.PLAIN, 24)); 

continuación se código de trabajo:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextPane; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Document; 
import javax.swing.text.SimpleAttributeSet; 
import javax.swing.text.StyleConstants; 

public class jeditorfont extends JFrame { 
    private JTextPane textPane = new JTextPane(); 

    public jeditorfont() { 
    super(); 
    setSize(300, 200); 

    textPane.setFont(new Font("Segoe UI", Font.PLAIN, 24)); 

    // create some handy attribute sets 
    SimpleAttributeSet red = new SimpleAttributeSet(); 
    StyleConstants.setForeground(red, Color.red); 
    StyleConstants.setBold(red, true); 
    SimpleAttributeSet blue = new SimpleAttributeSet(); 
    StyleConstants.setForeground(blue, Color.blue); 
    SimpleAttributeSet italic = new SimpleAttributeSet(); 
    StyleConstants.setItalic(italic, true); 
    StyleConstants.setForeground(italic, Color.orange); 

    // add the text 
    append("NULL ", null); 
    append("Blue", blue); 
    append("italic", italic); 
    append("red", red); 

    Container content = getContentPane(); 
    content.add(new JScrollPane(textPane), BorderLayout.CENTER); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    protected void append(String s, AttributeSet attributes) { 
    Document d = textPane.getDocument(); 
    try { 
     d.insertString(d.getLength(), s, attributes); 
    } catch (BadLocationException ble) { 
    } 
    } 

    public static void main(String[] args) { 
    new jeditorfont().setVisible(true); 
    } 
} 

ref: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetFontFontfont.htm

+1

Bueno, pero este es un ejemplo de 'JTextPane' –

16

Al renderizar HTML, fuente de JEditorPane necesita ser actualizado a través de su hoja de estilos:

JEditorPane editorPane = 
      new JEditorPane(new HTMLEditorKit().getContentType(),text); 
    editorPane.setText(text); 

    Font font = new Font("Segoe UI", Font.PLAIN, 24)); 
    String bodyRule = "body { font-family: " + font.getFamily() + "; " + 
      "font-size: " + font.getSize() + "pt; }"; 
    ((HTMLDocument)editorPane.getDocument()).getStyleSheet().addRule(bodyRule); 
19

prueba este:

JEditorPane pane = new JEditorPane(); 
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); 
pane.setFont(SOME_FONT); 

Todos los créditos a de-co-de blogger! Fuente: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

Acabo de probarlo. Esto hizo que JEditorPane utilizara la misma fuente que JLabel

JEditorPane pane = new JEditorPane(); 
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); 
pane.setFont(someOrdinaryLabel.getFont()); 

Funciona perfectamente.

1

Como está utilizando el kit de herramientas HTML, puede establecer la fuente en el HTML utilizando un estilo estándar. Así que cambiar el setText a algo como esto:

editorPane.setText("<html><head><style>" + 
        "p {font-family: Segoe UI; font-size:14;}" + 
        "</style></head>" + 
        "<body><p>It Works!</p></body></html>"); 

y quitar la instrucción setFont.

Cuestiones relacionadas