首页javajeditorpaneJava Swing - 如何在JEditorPane上显示图像

Java Swing - 如何在JEditorPane上显示图像

我们想知道如何在JEditorPane上显示图像。
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class Main extends JFrame {

  public static void main(String[] args) throws Exception {
    Main.createAndShowGUI();
  }

  private static void createAndShowGUI() throws IOException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String imgsrc = Main.class.getClassLoader().getSystemResource("a.jpg")
        .toString();
    frame.getContentPane().add(
        new JEditorPane("text/html", "<html><img src='" + imgsrc
            + "' width=200height=200></img>"));
    frame.pack();

    frame.setVisible(true);
  }
}