2013-02-26 77 views
1

我正在寫一些加載模板html文檔,做一些關鍵字替換,然後打印出來的東西。除了模板中包含圖像以外,這工作正常。如果我瀏覽到模板.html圖像顯示正常(所以我猜路徑正常),但它們在最終輸出中顯示爲空白空間。Swing - 當JEditorPane加載本地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

參見本[Q&A] (HTTP:// stackove rflow.com/q/15052690/230513)和[*使用JEditorPane *顯示圖像](http://www.javaworld.com/javatips/jw-javatip109.html)。 – trashgod 2013-02-26 03:59:59

回答

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

是的。就是這樣。 我的感謝! – Inf 2013-02-26 04:14:23

+0

+1個不錯的例子 – 2013-02-26 07:27:23