如何在 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); } }
输出
广告