首页javajeditorpaneJava Swing - 如何在阅读HTML时,在JEditorPane中应用ForegroundActions

Java Swing - 如何在阅读HTML时,在JEditorPane中应用ForegroundActions

我们想知道如何在阅读HTML时,在JEditorPane中应用ForegroundActions。
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLEditorKit;

public class Main extends JFrame {

  public Main() {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/HTML");
    editorPane.setEditorKit(new HTMLEditorKit());
    editorPane.setText("<hr>Welcome to <b>w3cschool.cn!</b><hr>");
    JToolBar bar = new JToolBar();
    bar.add(new StyledEditorKit.ForegroundAction("Red", Color.red));
    bar.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue));
    bar.add(new StyledEditorKit.FontSizeAction("12", 12));
    bar.add(new StyledEditorKit.FontSizeAction("14", 14));
    bar.add(new StyledEditorKit.FontSizeAction("16", 16));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.add(bar, BorderLayout.NORTH);
    this.add(editorPane, BorderLayout.CENTER);
    this.pack();
    this.setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
  }
}