简体   繁体   English

为什么JEditorPane不显示用HTML编写的图像?

[英]Why JEditorPane does not show image written in HTML?

So my HTML is this. 所以我的HTML是这个。

<!DOCTYPE HTML>
<html>
<body >
<h1 style="background-color:lightblue">This is a h1</h1>
<img src="haha.jpg" alt="W3Schools.com" width="104" height="142" > 
</p>
</body>
</html>

This is how I load the HTML: 这就是我加载HTML的方式:

JEditorPane je = new JEditorPane();
je.setEditable(false);
je.setContentType("text/html");
FileReader fr = new FileReader("testPage.html");
BufferedReader br = new BufferedReader(fr);
String text = "";
String inputLine = br.readLine();
while(inputLine!=null){
    text += inputLine+"\n";
    inputLine=br.readLine();
}
je.setText(text);
SwingNode sn = new SwingNode();
sn.setContent(je);

The h1 part works perfectly, but the image part does not show up, which shows up in .html file. h1部分可以正常工作,但是图像部分没有显示,而是显示在.html文件中。 So I want to know if there is any way to make image in HTML show up? 所以我想知道是否有任何方法可以显示HTML中的图像? If JEditorPane can't do it, what other component will show the html WITH images? 如果JEditorPane无法做到这一点,还有什么其他组件将显示html WITH图片?

Help Appreciated. 帮助表示赞赏。

I copied an image into the same working directory from where the program was executed and using the following HTML... 我将图像复制到执行程序的同一工作目录中,并使用以下HTML ...

<!DOCTYPE HTML>
<html>
    <body >
        <h1 style="background-color:lightblue">This is a h1</h1>
        <img src="haha.jpg" alt="W3Schools.com" width="104" height="142" > 
    </p>
</body>
</html>

And code... 和代码...

import java.awt.EventQueue;
import java.io.File;
import java.net.URL;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestEditor {

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

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

                JEditorPane editor = new JEditorPane();
                try {
                    editor.setPage(new File("testPage.html").toURI().toURL());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(editor));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Was able to get it to run without problems... 能够使其顺利运行...

编辑

Put the image in the relative location from the program execution context (".") 将图像放在程序执行上下文中的相对位置(“。”)

Since you're using SwingNode , your application actually uses JavaFX. 由于您使用的是SwingNode ,因此您的应用程序实际上使用的是JavaFX。

JavaFX has a web browser component, you can read more about it here: JavaFX具有Web浏览器组件,您可以在此处阅读有关它的更多信息:

Overview of the JavaFX WebView Component JavaFX WebView组件概述

Here's a short example how to use it: 这是一个简短的示例如何使用它:

WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load(url);

url can be an address like "http://example.com" or it can point to a file or resource like: url可以是诸如"http://example.com"的地址,也可以指向诸如以下文件或资源:

Paths.get("testPage.html").toURI().toURL();

If you have the HTML content as a String , you can load it like this: 如果您将HTML内容作为String ,则可以这样加载:

String htmlContent = "<html>....</html>";
browser.getEngine().loadContent(htmlContent);

If you want to insert images in the htmlContent , you can do it like this: 如果要在htmlContent插入图像,可以这样进行:

URL url = Main.class.getResource("haha.jpg"); // haha.jpg is next to Main class
String text = "<html><img src=\"" + url + "\" width=\"104\" height=\"142\" > </html>";

Or if you want to insert an image pointing to a fixed file on your computer, create the url like this: 或者,如果您要在计算机上插入指向固定文件的图像,请创建如下url

URL url = new File("C:/images/haha.jpg").toURI().toURL();

Or: 要么:

URL url = Paths.get("C:/images/haha.jpg").toUri().toURL();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM