如何使用 HTML 在 JEditorPane 中显示图像
如果要使图像显示在包含 HTML 代码的
JEditorPane 中,此解决方案工作正常:
import javax.swing.*;
导入 java.awt.*;
class Image extends JFrame {
public static void main(String[] args) throws Exception {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
字符串 src = Image.class.getClassLoader().
getSystemResource(image.jpg”).toString();
frame.getContentPane().add(new JEditorPane(text/html”,
"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
输出:
.jpg映像必须放在 EditorPaneImage.class 文件中的同一目录中。编译代码时,java 会调用方法 getSystemResource(jpgname)。优点是,当您从 .jar 文件创建和运行时,应用程序可以很好地运行。