JEdi​​torPane java 中的 HTML 显示问题

发布于 2024-10-01 03:06:49 字数 508 浏览 1 评论 0原文

我正在尝试在 JEditorPane 中显示 html。最初类型设置为“text/html”。

当我使用 setPage(URL) 时,它工作正常并显示结果输出,但如果我有一个包含 HTML 代码的字符串,并且我使用 setText(String) 在 JEditorPane 上显示结果,则不会显示任何内容,我只看到空白。

当然,如果我复制字符串中的内容,将其粘贴到 notpad 中,将其另存为 .html,然后在浏览器中打开生成的文件,它会正确显示。真正的问题在于 JEditorPane 如何或如何处理字符串以显示其中的内容。 JEdi​​torPane 位于 JscrollPane 内,JscrollPane 位于 Jframe 内。我只使用 setContentType( "text/html" ) 和 setText(String) 方法来显示 html。

除了将生成的 html 代码写入文件并使用 SetPage(URL) 之外,还有什么办法可以解决这个问题吗?如果您需要的话,我可以发布 html 代码(但它很大)。感谢您的帮助。

Im trying to display html in a JEditorPane. Initially the type is set to "text/html".

When I use setPage(URL) it works fine and the resulting output is displayed but If I have a String that contains HTML code and I used setText(String) to display the result on the JEditorPane nothing is displayed I see only white space.

Of-course if I copied the whats in the string pasted it in notpad, saved it as .html then opened the resulting file in the browser it displayed correctly. The real problem is in how or what the JEditorPane does with the string inorder to display whats inside it. The JEditorPane is inside a JscrollPane which is inside a Jframe. and I only used setContentType( "text/html" ) and setText(String) methods for html display.

Is there anyway to get around this than wrting the resulting html code to a file and using SetPage(URL)? I can post the html code if you need it (but its quite large). Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2024-10-08 03:06:49

不知道为什么 setText 不起作用。但这里有一个解决方法。

试试这个网址。 (URL 中的整个文件)(这是 Android 的 WebView 在其中 setText 时调用的内容)

data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0D%0A

它以 data:text/html;charset=utf-8, 开头,后面是您的 HTML。

但是你必须对其进行编码。至少你必须将 % 替换为 %25 其余的可能无需编码即可工作。

您还可以使用此代码嵌入图像,而无需调用文件。

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC" />

您只需对图像进行 Base64 编码,然后就可以将其粘贴进去。

Don't know why setText does not work. But here is a workaround.

Try this URL. (the whole file in the URL) (This is what Android's WebView calls when you setText in it)

data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0D%0A

It starts with data:text/html;charset=utf-8, and is followed by your HTML.

However you do have to encode it.. At least you have to replace % with %25 The rest might just work without encoding though.

You can also use this code to embed images without calling a file

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC" />

You just have to base64 encode your image and then you can paste it right in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文