【问题标题】:Swing - local images not displaying when JEditorPane loads a local htmlSwing - JEditorPane 加载本地 html 时不显示本地图像
【发布时间】:2013-02-26 12:06:16
【问题描述】:

我正在写一些东西,它加载一个模板 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...
}

文本显示和格式正确,只是图像从不显示。 谁能发现哪里出了问题?

干杯。

【问题讨论】:

标签: java swing


【解决方案1】:

我认为路径是错误的。

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

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

我能够让它工作......

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) {
                    }
                }
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2017-02-14
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    相关资源
    最近更新 更多