我已经搜索过,也没见过任何人体验到我是什么样的人。然而,这正是我面临的问题:
我正在尝试将一小部分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之后,我的编辑器窗格内容如下:
<html>
<head>
</head>
<body>
</body>
</html>我尝试过将<html>和</html>添加到字符串的开头和结尾,但没有成功。有人知道我错过了什么或者做错了什么吗?
我使用的是Java 1.7.0_55 32位.
发布于 2014-07-24 00:33:29
经过一些测试我发现..。
JEditorPane接受它之前,HTML必须有良好的格式,而且实际上,它似乎完成了它自己的一些验证,删除了无效的tags...fun内容<tr><td>...</td></tr>包含到表中。JEditorPane中下载,尽管相同的HTML会在浏览器中加载该图像(如Chrome)。1,并向图像添加了一个alt标记,这有助于验证某些实际呈现的元素.

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);
}
});
}
}https://stackoverflow.com/questions/24923129
复制相似问题