JEdi​​torPane 不设置任何 HTML

See*_*hor 1 html java swing image jeditorpane

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

我试图将一小段 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);
Run Code Online (Sandbox Code Playgroud)

但是在我调用 setText 之后,我的编辑器窗格内容是:

<html>
  <head>

  </head>
  <body>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

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

我使用的是 Java 1.7.0_55 32 位。

Mad*_*mer 5

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

  • HTML必须在JEditorPane接受它之前格式良好,事实上,它似乎做了一些自己的验证,删除无效标签......有趣的东西
  • 我必须将表格行和单元格<tr><td>...</td></tr>包含在表格中
  • 如果 HTTP 标头没有适当的标头,某些站点可能会主动阻止图像的下载,这意味着您在示例中JEditorPane的图像在 .像铬)
  • 有时在 HTML 中添加其他内容以确保呈现您的想法会很有帮助,例如,我只是添加了一些文本,将表格边框设置为 并向图像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);
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)