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

To nie zmienia czcionki tekstu. Muszę wiedzieć, jak ustawić domyślną czcionkę dla JEditorPane za pomocą HTML Editor Kit.Ustawienie domyślnej czcionki w JEditorPane

Edit:

enter image description here

+4

Proszę napisać swój kod w formacie tekstowym, a nie jego wizerunku, ponieważ każdy, kto chce go przetestować, musi to napisać. To nie jest szkoła :) –

+0

Więcej o tym, jak zrobić zrzut ekranu (http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustratea-a- stanowisko). – trashgod

Odpowiedz

1

Sprawdziłem kod, nie powinno być żadnego problemu. Czy przetestowałeś inne czcionki? Proszę wypróbuj czcionkę "Segoe Script" i sprawdź, czy się zmieni.

Edytuj: Próbowałem kod poniżej, to działa dobrze dla mnie. Czy jesteś pewien, że opublikowany przez Ciebie kod jest dokładnie taki sam, jak zaimplementowałeś?

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

Edit2: Zmień swoją główną metodę następująco. Ustawia Nimbus LookAndFeel. Nie sprawdziłem jeszcze innych 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

kod działa, ale czcionka nie jest aktualizowana. – Sanjeev

+0

jak już wspomniałem w mojej edytowanej odpowiedzi, upewnij się, że zaimplementowałeś dokładny kod –

+0

, proszę obserwuj przesłany obraz, ponieważ tekst nie jest czcionką segoe. – Sanjeev

1

try poniżej

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

poniżej kod działa:

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

Nice, ale jest to przykład 'JTextPane' –

16

Podczas renderowania HTML, potrzebuje czcionki JEditorPane, który ma być aktualizowany za pośrednictwem arkusza stylów:

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

Spróbuj tego:

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

Wszystkie kredyty do de-ko-de blogera! Źródło: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

Właśnie to przetestowałem. To sprawiło JEditorPane używać tej samej czcionki jak JLabel

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

działa idealnie.

1

Podczas korzystania z zestawu narzędzi HTML można ustawić czcionkę w kodzie HTML przy użyciu standardowej stylizacji. Więc zmień setText na coś takiego:

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

i usuń instrukcję setFont.

Powiązane problemy