在 jeditorpane (java swing) 上显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/573055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
showing images on jeditorpane (java swing)
提问by Giancarlo
I have a JEditorPane created by this way:
我有一个通过这种方式创建的 JEditorPane:
JEditorPane pane = new JEditorPane("text/html", "<font face='Arial'>" + my_text_to_show + "<img src='/root/img.gif'/>" + "</font>");
I put this pane on a JFrame.
我将此窗格放在 JFrame 上。
Text is shown correctly, but I can't see the picture, there is only a square indicating that there should be an image (i.e.: "broken image" shown by browsers when picture has not been found)
文字显示正确,但我看不到图片,只有一个方块表示应该有图片(即:浏览器未找到图片时显示的“损坏的图片”)
回答by michal
You have to provide type, and get the resource. That's all. My tested example, but I'm not sure about formating. Hope it helps:
您必须提供类型并获取资源。就这样。我的测试示例,但我不确定格式化。希望能帮助到你:
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
public class Test extends JFrame {
public static void main(String[] args) throws Exception {
Test.createAndShowGUI();
}
private static void createAndShowGUI() throws IOException {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String imgsrc =
Test.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);
}
}
回答by John Gardner
The JEditorPane is using HTMLDocument.getBase to locate relative urls as well, so if you are displaying content from a directory, make sure to set the base on the html document so it resolves urls relative to the base directory.
JEditorPane 也使用 HTMLDocument.getBase 来定位相对 url,因此如果您要显示目录中的内容,请确保在 html 文档上设置 base,以便它解析相对于基本目录的 url。
Depending on where that image actually is, you might want to extend HTMLEditorKit+HTMLFactory+ImageView and provide a custom implementation of ImageView, which is responsible for mapping the attribute URL to the image URL, too.
根据图像的实际位置,您可能希望扩展 HTMLEditorKit+HTMLFactory+ImageView 并提供 ImageView 的自定义实现,它也负责将属性 URL 映射到图像 URL。
回答by ahh22
None of the above worked for me, however 'imgsrc = new File("passport.jpg").toURL().toExternalForm();'let me to try and have each image in the html have a preceding 'file:' so that it now reads:
以上都不适合我,但是 'imgsrc = new File("passport.jpg").toURL().toExternalForm();'让我尝试让 html 中的每个图像都有一个前面的“文件:”,以便它现在显示:
<img src="file:passport.jpg" />
And that works fine for me.
这对我来说很好用。
回答by oozzal
If you want to specify relative path to the image.
如果要指定图像的相对路径。
Let's say your project folder structure is as following:
假设您的项目文件夹结构如下:
sample_project/imagessample_project/images/loading.gifsample_project/srcsampler_project/src/package_name
sample_project/imagessample_project/images/loading.gifsample_project/srcsampler_project/src/package_name
Now the image tag would look like this:"<img src='file:images/loading.gif' width='100' height='100'>"
现在图像标签看起来像这样:"<img src='file:images/loading.gif' width='100' height='100'>"
Yaay!
耶!
回答by Peter
I used this when I was working in netbeans, it worked though. I think a little modification if the program should run outside of netbeans,
我在 netbeans 工作时使用了它,但它确实有效。如果程序应该在 netbeans 之外运行,我想稍微修改一下,
String imgsrc="";
try {
imgsrc = new File("passport.jpg").toURL().toExternalForm();
} catch (MalformedURLException ex) {
Logger.getLogger(EntityManager.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println(imgsrc); use this to check
html = "<img src='" + imgsrc + "' alt='' name='passport' width='74' height='85' /><br />";
//use the html ...
if you run from the jar, the image file has to be on the same directory level, ... in fact, the image file has to be on the same directory as your execution entry.
如果您从 jar 运行,则图像文件必须位于同一目录级别,...实际上,图像文件必须与您的执行条目位于同一目录中。

