การตั้งค่าแบบอักษรเริ่มต้นใน JEditorPane

editorPane.setContentType("text/html");    
editorPane.setFont(new Font("Segoe UI", 0, 14));
editorPane.setText("Hello World");

สิ่งนี้จะไม่เปลี่ยนแบบอักษรของข้อความ ฉันต้องการทราบวิธีตั้งค่าแบบอักษรเริ่มต้นสำหรับ JEditorPane ด้วย HTML Editor Kit

แก้ไข:

ป้อนคำอธิบายรูปภาพที่นี่


person Sanjeev    schedule 22.09.2012    source แหล่งที่มา
comment
กรุณาโพสต์โค้ดของคุณในรูปแบบข้อความ ไม่ใช่รูปภาพ เนื่องจากใครก็ตามที่ต้องการทดสอบจะต้องเขียนออกมา ที่นี่ไม่ใช่โรงเรียน :)   -  person David Kroukamp    schedule 22.09.2012
comment
ข้อมูลเพิ่มเติมเกี่ยวกับวิธีใช้ภาพหน้าจอ.   -  person trashgod    schedule 22.09.2012


คำตอบ (5)


ลองอันนี้:

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

เครดิตทั้งหมดให้กับบล็อกเกอร์ de-co-de! ที่มา: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

ฉันเพิ่งทดสอบมัน สิ่งนี้ทำให้ JEditorPane ใช้ฟอนต์เดียวกันกับ JLabel

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

ทำงานได้อย่างสมบูรณ์แบบ

person Espinosa    schedule 02.03.2013

เมื่อแสดงผล HTML แบบอักษรของ JEditorPane จะต้องได้รับการอัปเดตผ่านสไตล์ชีต:

    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);
person bobby_light    schedule 22.09.2012

ขณะที่คุณกำลังใช้ชุดเครื่องมือ HTML คุณสามารถตั้งค่าแบบอักษรใน HTML โดยใช้สไตล์มาตรฐาน ดังนั้นเปลี่ยน setText เป็นดังนี้:

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

และลบคำสั่ง setFont

person IanB    schedule 28.09.2017

ลองด้านล่าง

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

ด้านล่างเป็นรหัสการทำงาน:

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);
  }
}

อ้างอิง: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetFontFontfont.htm

person Alvin Pradeep    schedule 22.09.2012
comment
ดีแต่นี่คือตัวอย่างของ JTextPane - person David Kroukamp; 22.09.2012

ฉันตรวจสอบรหัสของคุณแล้ว ไม่น่าจะมีปัญหาใดๆ คุณได้ทดสอบแบบอักษรอื่น ๆ แล้วหรือยัง? โปรดลองใช้แบบอักษร "Segoe Script" และดูว่ามีการเปลี่ยนแปลงหรือไม่

แก้ไข: ฉันได้ลองใช้โค้ดด้านล่างนี้แล้ว ซึ่งทำงานได้ดีสำหรับฉัน คุณแน่ใจหรือไม่ว่าโค้ดที่คุณโพสต์นั้นเหมือนกับโค้ดที่คุณใช้งานทุกประการ

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

แก้ไข2: เปลี่ยนวิธีการหลักของคุณดังต่อไปนี้ โดยจะตั้งค่า Nimbus LookAndFeel ฉันยังไม่ได้ตรวจสอบ 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();
        }
    });
}
person Canis Majoris    schedule 22.09.2012
comment
ดังที่ฉันได้กล่าวไว้ในคำตอบที่แก้ไขแล้วตรวจสอบให้แน่ใจว่าคุณได้ติดตั้งโค้ดที่ถูกต้อง - person Canis Majoris; 22.09.2012
comment
โปรดสังเกตภาพที่อัปโหลด เนื่องจากข้อความไม่ใช่แบบอักษร segoe script - person Sanjeev; 22.09.2012
comment
บางทีแพลตฟอร์มของคุณอาจไม่รองรับ ระบบปฏิบัติการของคุณคืออะไร? ดูโพสต์ของฉันอีกครั้ง ฉันแสดงให้คุณเห็นวิธีการตั้งค่า Nimbus LookAndFeel ลองแล้ว มันต้องได้ผล - person Canis Majoris; 22.09.2012