Swing - 当JEditorPane加载本地html时不显示本地图像

时间:2013-02-26 03:31:15

标签: java swing

我正在写一些加载模板html文档的东西,在其上做一些关键字替换,然后将其打印出来。除了模板中包含图像之外,这种方法很好。如果我浏览模板.html图像显示正常(所以我猜路径没问题),但它们在最终输出中显示为空白区域。

模板html类似于:

<html>
<body>
<img src="file://c:/temp/my-logo.png" width="50" height="50"/>
[[[some stuff I want to replace]]]
</body>
</html>

足够简单。 并加载此模板:

public void test() {
   JEditorPane text = new JEditorPane("text/html", "default");
   HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
   HTMLDocument htmlDocument = (HTMLDocument)htmlEditorKit.createDefaultDocument();

   text.setEditorKit(htmlEditorKit);
   // read the html template into the JEditorPane's text
   text.read(new BufferedReader(new InputStreamReader(new FileInputStream(new File("path to my template html")))), htmlDocument);

   // then do some replacements
   text.setText(magicReplacements(text.getText()));

   text.repaint();
   // and then print job stuff, fire off the job, check if it worked etc...
}

文本正确显示和格式化,只显示图像。 谁能发现什么是错的?

干杯。

1 个答案:

答案 0 :(得分:6)

我认为路径是错误的。

而不是file://c:/temp/my-logo.png,您应该尝试file:/c:/temp/my-logo.png

在测试我的示例时,我使用了//c://c:/,第二个使用了,第一个失败了。

我能够让这个工作......

enter image description here

<强> HTML

<html>
<body>
<img src="file:/c:/backgroundtext.png"/>
[[[some stuff I want to replace]]]
</body>
</html>

示例代码

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

public class TestTextPane {

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

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

                FileReader fr = null;

                try {
                    fr = new FileReader(new File("c:/Test.html"));
                    JEditorPane editor = new JEditorPane();
                    editor.setContentType("text/html");
                    editor.read(fr, "Test");

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(editor));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                } finally {
                    try {
                        fr.close();
                    } catch (Exception e) {
                    }
                }
            }
        });
    }
}