HTMLを使用してJEditorPaneに画像を表示する方法
HTMLコードを含む
JEditorPaneに画像を表示させたい場合、このソリューションは問題なく動作します:
import javax.swing.*;
import 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);
}
}
Output:
.jpgイメージは、EditorPaneImage.class ファイルの同じディレクトリに配置する必要があります。コードがコンパイルされると、javaはメソッドgetSystemResource(jpgname)です。この利点は、.jar ファイルから作成して実行すると、アプリケーションが非常に適切に動作することです。