java 如何在 JTextPane 中添加图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9993424/
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
How to add images in JTextPane?
提问by Suhail Ahamed
I want to give the user the facility to copy and paste Imagesin JTextPane
. Please help me.
我想为用户提供将图像复制和粘贴
到JTextPane
. 请帮我。
回答by Mikle Garin
There is an easy way to add an image:
有一种简单的方法可以添加图像:
JTextPane pane = new JTextPane ();
pane.insertIcon ( new ImageIcon ( "/path/to/image.png" ) );
But there is no simple way to copy an image from the pane, since it cannot be selected and cannot be easily located in the pane document.
但是没有简单的方法可以从窗格中复制图像,因为它不能被选择,也不能在窗格文档中轻松定位。
回答by Joop Eggen
You need a StyledDocument, like resulting from HTML. Hence set the content type to "text/html". Then <img src="file:...">
will link to an image on the file system.
您需要一个 StyledDocument,就像从 HTML 生成的一样。因此将内容类型设置为“text/html”。然后<img src="file:...">
将链接到文件系统上的图像。
Then you can provide drag-and-drop or paste from File or Image (the latter you have to save in your own files).
然后您可以从文件或图像(后者您必须保存在您自己的文件中)提供拖放或粘贴。