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

Dies ändert nicht die Schriftart des Texts. Ich muss wissen, wie man die Standardschrift für den JEditorPane mit HTML-Editor-Kit einstellt.Festlegen der Standardschriftart in JEditorPane

Edit:

enter image description here

+4

Bitte schreiben Sie Ihren Code im Textformat und nicht ein Bild davon, denn jeder, der es testen möchte, muss es aufschreiben. Dies ist keine Schule :) –

+0

Mehr dazu, wie man ein [screenshot] (http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a- Post). – trashgod

Antwort

1

ich Ihren Code überprüft haben, sollte es kein Problem sein. Haben Sie andere Schriftarten getestet? Bitte versuchen Sie "Segoe Script" Schriftart und sehen Sie, ob es sich ändert.

Bearbeiten: Ich habe versucht, den Code unten, es funktioniert gut für mich. Sind Sie sicher, dass der von Ihnen veröffentlichte Code genau dem entspricht, den Sie implementiert haben?

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

Edit2: Ihre Hauptmethode wie folgt ändern. Es setzt das Nimbus LookAndFeel. Ich habe noch keine anderen LookAndFeels überprüft.

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

der Code funktioniert, aber die Schriftart wird nicht aktualisiert. – Sanjeev

+0

wie ich in meiner bearbeiteten Antwort erwähnt, stellen Sie sicher, dass Sie den genauen Code implementiert haben –

+0

Bitte beachten Sie das hochgeladene Bild, da der Text nicht von der Schriftart Segoe Script ist. – Sanjeev

1

Versuch unter

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

unten arbeitet Code:

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

Schön, aber das ist ein Beispiel für 'JTextPane' –

16

Wenn HTML-Rendering, JEditorPane die Schriftart muss über seinen Stylesheet aktualisiert werden:

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

dieses Versuchen:

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

Alle Kredite an de-co-de Blogger! Quelle: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

Ich habe es gerade getestet. Dies machte JEditorPane die gleiche Schriftart wie JLabel verwenden

Funktioniert perfekt.

1

Während Sie das HTML-Toolkit verwenden, können Sie die Schriftart im HTML-Format mit dem Standardstyling festlegen. So ändern Sie den setText zu etwas wie diesem:

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

und entfernen Sie die setFont-Anweisung.