如何在 Java Swing JEditorPane 中使用 HTML 更改字型大小?


使用 HTMLEditorKitt 根據 HTML 更改字型大小。根據這一點,使用 JEditorPane setText() 方法設定 HTML

HTMLEditorKit kit = new HTMLEditorKit();
editorPane.setEditorKit(kit);
editorPane.setSize(size);
editorPane.setOpaque(true);
editorPane.setText("<b><font face=\"Verdana\" size=\"30\"> This is a demo text with a different font!</font></b>");

以下是一個示例,說明如何在 Java Swing JEditorPane 中使用 HTML 更改字型大小

示例

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;
public class SwingDemo extends JFrame {
   public static void main(String[] args) {
      SwingDemo s = new SwingDemo();
      s.setSize(600, 300);
      Container container = s.getContentPane();
      s.demo(container, container.getSize());
      s.setVisible(true);
   }
   public void demo(Container container, Dimension size) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      HTMLEditorKit kit = new HTMLEditorKit();
      editorPane.setEditorKit(kit);
      editorPane.setSize(size);
      editorPane.setOpaque(true);
      editorPane.setText("<b><font face=\"Verdana\" size=\"30\"> This is a demo text with a different font!</font></b>");
      container.add(editorPane, BorderLayout.CENTER);
   }
}

輸出

更新時間:30-Jul-2019

755 檢視人數

開啟你的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.