gpt4 book ai didi

java - JEdi​​torPane 不设置任何 HTML

转载 作者:行者123 更新时间:2023-12-01 18:29:29 25 4
gpt4 key购买 nike

我已经搜索过,但还没有看到有人遇到我的情况..,但这就是我遇到的问题:

我正在尝试将一小段 HTML 设置为 JEditorPane 的文本。这是代码:

JEditorPane htmlPane = new JEditorPane();
String imageString = "<img src=\"http://tfwiki.net/mediawiki/images2/thumb/3/37/Optimusg1.jpg/350px-Optimusg1.jpg\"/>";
String description = "<table width=300 border=0 cellspacing=0></table>" + imageString + "</table>";
htmlPane.setContentType("text/html");
htmlPane.setText(description);

但是在我调用 setText 后,我​​的编辑器 Pane 内容是:

<html>
<head>

</head>
<body>
</body>
</html>

我尝试过添加 <html> 的变体和</html>到我的字符串的开头和结尾,但没有运气。有人知道我错过了什么或做错了什么吗?

我使用的是 Java 1.7.0_55 32 位。

最佳答案

经过一些测试,我发现......

  • HTML 必须在 JEditorPane 之前格式良好。会接受它,事实上,它似乎做了一些自己的验证,删除无效标签......有趣的东西
  • 我必须包含表格行和单元格 <tr><td>...</td></tr>进入表格
  • 如果 HTTP header 没有适当的 header ,某些网站可能会主动阻止图像下载,这意味着您示例中的图像在 JEditorPane 中反复下载失败。 ,即使相同的 HTML 会在浏览器(如 Chrome)中加载图像
  • 有时在 HTML 中添加其他内容会很有帮助,以确保它呈现您所想的内容,例如,我只是添加了一些文本,将表格边框设置为 1并添加 alt标记到图像,这有助于验证实际渲染的某些元素...

Editor

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestEditorPane {

public static void main(String[] args) {
new TestEditorPane();
}

public TestEditorPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JEditorPane htmlPane = new JEditorPane();
String description = "<html><body>Hello<table border=1><tr><td><img alt='Bad' src='http://fc07.deviantart.net/fs70/i/2012/084/c/0/angry_wet_ponies_are_angry____by_tabby444-d4tyfsc.png'/></tr></td></table></body></html>";
htmlPane.setContentType("text/html");
htmlPane.setText(description);
System.out.println(htmlPane.getText());

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(htmlPane));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

关于java - JEdi​​torPane 不设置任何 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24923129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com