Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么JEditorPane不显示用HTML编写的图像?_Java_Html_Swing_Javafx_Jeditorpane - Fatal编程技术网

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

Java 为什么JEditorPane不显示用HTML编写的图像?,java,html,swing,javafx,jeditorpane,Java,Html,Swing,Javafx,Jeditorpane,我的HTML就是这样 这是h1 这是我加载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 inputLin

我的HTML就是这样


这是h1

这是我加载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);
h1部分工作得很好,但是图像部分没有显示,它显示在.html文件中。所以我想知道是否有任何方法可以让HTML中的图像显示出来?如果
JEditorPane
无法执行此操作,那么其他哪个组件将显示带有图像的html


谢谢你的帮助

因为您使用的是
SwingNode
,所以您的应用程序实际上使用的是JavaFX

JavaFX有一个web浏览器组件,您可以在此处了解更多信息:

下面是一个如何使用它的简短示例:

WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load(url);
url
可以是类似于
的地址http://example.com“
也可以指向以下文件或资源:

Paths.get("testPage.html").toURI().toURL();
如果HTML内容为
字符串
,则可以按如下方式加载:

String htmlContent = "<html>....</html>";
browser.getEngine().loadContent(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>";
URL url = new File("C:/images/haha.jpg").toURI().toURL();
或者,如果要在计算机上插入指向固定文件的图像,请按如下方式创建
url

String htmlContent = "<html>....</html>";
browser.getEngine().loadContent(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>";
URL url = new File("C:/images/haha.jpg").toURI().toURL();
或:


我将一个图像复制到执行程序的同一工作目录中,并使用以下HTML


这是h1

和代码

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

}
能够让它毫无问题地运行


将图像放在程序执行上下文中的相对位置(“.”

可能是因为它找不到图像,它与运行它的代码的关系存储在哪里?我使用了绝对文件路径。在运行时,JEditorPane为图像保存了一个空间,但图像刚刚被破坏……可能是重复的@andrewhompson,但没有嵌入的资源?顺便说一下-
这是一个h1
Java(HTML呈现)不理解
lightblue
。试试看,我用的是JavaFX。当我改用editor.setPage(新文件(“testPage.html”).toURI().toul()时;它显示了一个质量。那么你为什么使用<代码> JEdError Prase<代码>?如果你使用<代码> JavaFX为什么不使用<代码> WebVIEW ?@ YvaNoVSKI考虑提供一个演示你的问题的方法。这将减少混乱和更好的响应,直到不起作用。我使用了browser.getEngine().loadContent(文本);该图像未成功加载…在使用html文件中的相对路径后,我获得了它!但是当我在html文件中使用绝对路径时,比如@Yvainovski,它为什么不能工作,很可能是因为您省略了冒号:
“file:/c:/bla/bla/bla/haha.jpg”
新文件(“c:/bla/bla/bla/haha.jpg”).toURI().toul().toString()
的结果。另外,当我使用(我们是WebEngine)时,我们加载内容(text);它只显示了图像的边缘。为什么?@Yvainovski
WebEngine.loadContent()
需要的是HTML字符串,而不是URL!如果您有地址(url),请使用
WebEnigne.load(url)