使用Java中的HTMLEditorKit,如何查找本地文件路径<;img src=…>;标签将使用什么?

使用Java中的HTMLEditorKit,如何查找本地文件路径<;img src=…>;标签将使用什么?,java,html,image,swing,htmleditorkit,Java,Html,Image,Swing,Htmleditorkit,我已经运行了一个java应用程序,我正在使用HTMLDocument/HTMLEditorKit功能来构建我的聊天室。到目前为止,在构建我的样式表、插入文本消息等方面取得了很大的成功 所以现在我来。。。图像!我曾想过,如果我正在添加标签,例如: <img src="myimage.png"> 。。。但是没有快乐 在这里寻找某种相对路径,这样最终与应用程序一起安装在用户机器上的图像文件就能显示出来 [编辑:哎呀,修复了我的HTML示例] 有没有办法问它认为它是从哪个目录读取的 不知

我已经运行了一个java应用程序,我正在使用HTMLDocument/HTMLEditorKit功能来构建我的聊天室。到目前为止,在构建我的样式表、插入文本消息等方面取得了很大的成功

所以现在我来。。。图像!我曾想过,如果我正在添加标签,例如:

<img src="myimage.png"> 
。。。但是没有快乐

在这里寻找某种相对路径,这样最终与应用程序一起安装在用户机器上的图像文件就能显示出来

[编辑:哎呀,修复了我的HTML示例]

有没有办法问它认为它是从哪个目录读取的

不知道它是否应该在类文件所在的同一目录(或子目录)中,或者它是否可以在类路径上找到的任何目录中

在任何情况下,您的HTML都是错误的:

<img="myimage.png"> 

或者调整图像的大小

<img src="myimage.png"> 


阅读上的Swing教程中的部分。
TextSamplerDemo
有显示图像的工作代码。

好的,我发现(从一些与其他问题相关的答案中收集到的花边新闻)我需要创建一个URL项,然后用ClassLoader或类似的方法填充它

例如,一个人把他的东西带到了一起:

<img src="myimage.png" width="64" height="64">
String filename=getClass().getClassLoader().getResource(“res/Kappa.png”).toString();
String preTag=“文件名为:“+filename+”;
字符串imageTag=“”;
insertHTML(doc,doc.getLength(),preTag+imageTag,0,0,HTML.Tag.IMG);
同时,根据这个提示,最终让我的东西运行“最好”的方法是:

URL;
字符串keystring=“+s+”,0,0,null);
}捕获(BadLocationException-ble){
ErrorDialog.bug(ble);
}捕获(IOEX异常){
ErrorDialog.bug(ex);
}
conversation.update(conversation.getGraphics())//BR//强制更新图形
我的软件包在VASSAL(游戏平台)下运行,因此能够从我的VASSAL模块的数据存档(zip文件)中提取东西对我来说非常理想,尽管当时我问了这个问题,如果我能找到一个图像文件并将其从数据存档中取出,我会很高兴

您可以看到,我的策略是让html(可能由另一个模块设计器生成,而不是我)在源标记(例如src=“dice.png”)中引用简单的文件名,如果成功,我将解析这些文件名并用URL查询的结果替换它们


如果有人对原始问题(HTMLEditorKit到底在哪里查找本地路径?--默认的本地路径是什么?我如何使它在我当前的工作目录、应用程序运行的目录或任何类似的本地目录中查找)有更完整的答案,那么请将它发布到这里,以供子孙后代使用,因为尽管这让我跑了起来,但我觉得这并不是对原始问题的全面回答。

啊,哎呀,我真的是太匆忙了。将签出您的链接。TextSamplerDemo不使用HTMLEditorKit显示图像。这就是我需要的特定功能(或者为了了解通过HTMLEditorKit实际上不可能实现),演示使用JEditorPane使用HTMLEditorKit。您只需使用
getEditorKit()
方法访问编辑器工具包。好的,谢谢。在上面的问题中,我主要搜索的是如何让我的路径工作来查找图像,因为如果我能用html处理图像,这对我来说会有用得多。后来我发现(从其他与此相关的问题的片段中)如果我生成URL项,例如使用ClassLoader.getResource()类型的方法。我希望有人能顺道过来,给出那种类型的完整答案,否则我会在几天内提供我自己的部分答案。有关提示,请参阅。
<img src="myimage.png" width="64" height="64">
String filename = getClass().getClassLoader().getResource("res/Kappa.png").toString();
String preTag="<PRE>filename is : "+filename+"</PRE>";
String imageTag="<img src=\""+filename+"\"/>";
kit.insertHTML(doc, doc.getLength(), preTag+imageTag, 0, 0, HTML.Tag.IMG);
    URL url;
    String keystring = "<img src=\"";
    String file, tag, replace;
    int base;
    while (s.toLowerCase().contains(keystring)) { // Find next key (to-lower so we're not case sensitive)
        //There are undoubtedly more efficient ways to do this parsing, but this miraculously ran perfectly the very first time I compiled it, so, you know, superstition :)
        base = s.toLowerCase().indexOf(keystring);
        file = s.substring(base + keystring.length(), s.length()).split("\"")[0]; // Pull the filename out from between the quotes
        tag  = s.substring(base, base + keystring.length()) + file + "\""; // Reconstruct the part of the tag we want to remove, leaving all attributes after the filename alone, and properly matching the upper/lowercase of the keystring                        

        try {
            url = GameModule.getGameModule().getDataArchive().getURL("images/" + file);
            replace = "<img  src=\"" + url.toString() + "\""; // Fully qualified URL if we are successful. The extra
                                                                // space between IMG and SRC in the processed
                                                                // version ensures we don't re-find THIS tag as we
                                                                // iterate.
        } catch (IOException ex) {
            replace = "<img  src=\"" + file + "\""; // Or just leave in except alter just enough that we won't find
                                                    // this tag again.
        }

        if (s.contains(tag)) {
            s = s.replaceFirst(tag, replace); // Swap in our new URL-laden tag for the old one.
        } else {
            break; // BR// If something went wrong in matching up the tag, don't loop forever
        }
    }

    //BR// Insert a div of the correct style for our line of text. 
    try {
        kit.insertHTML(doc, doc.getLength(), "\n<div class=" + style + ">" + s + "</div>", 0, 0, null);
    } catch (BadLocationException ble) {
        ErrorDialog.bug(ble);
    } catch (IOException ex) {
        ErrorDialog.bug(ex);
    }
    conversation.update(conversation.getGraphics()); //BR// Force graphics to update