gpt4 book ai didi

image - 在 JavaFX HTMLeditor 中设置本地镜像

转载 作者:行者123 更新时间:2023-12-02 01:19:23 26 4
gpt4 key购买 nike

我正在寻找一种使用 JavaFX HTMLEditor 的 setHtmlText 添加本地图像的方法。我可以添加远程图像没有问题:

HTMLEditor editor = new HTMLEditor();  
editor.setHtmlText("<img src=\"http://someaddress.com\" width=\"32\" height=\"32\" >");

但无法对本地镜像执行此操作

HTMLEditor editor = new HTMLEditor();  
editor.setHtmlText("<img src=\"categoryButton.fw.png\" width=\"32\" height=\"32\" >");

该按钮与java源代码处于同一级别。那么为什么这行不通呢?

最佳答案

使用getResource获取本 map 片资源的位置。

editor.setHtmlText(
"<img src=\"" +
getClass().getResource("categoryButton.fw.png") +
"\" width=\"32\" height=\"32\" >"
);

它的工作方式与将内容加载到 WebView 中相同。如:

How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?

<小时/>

这是屏幕截图:

editorwithimage

还有一个可执行示例:

import javafx.application.*;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class HTMLEditorWithImage extends Application {
@Override public void start(Stage stage) {
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText(
"<img src=\"" +
getClass().getResource("Blue-Fish-icon.png") +
"\" width=\"32\" height=\"32\" >"
);
stage.setScene(new Scene(editor));
stage.show();
}

public static void main(String[] args) { launch(args); }
}
<小时/>

I was just curious if this was the only way of getting an image into a some sort of text area?

  1. 使用 JavaFX 2,您可以将与文本混合的图像嵌入 FlowPaneJavafx Text multi-word colorization 中所述.
  2. Java 8 添加了 TextFlow允许您将图像嵌入到文本区域的组件。

以上两种技术仅用于数据显示。两者都不允许编辑插入图像和其他节点的多样式文本。目前,JavaFX 平台为此功能提供的唯一控件是 HTMLEditorWebViewcontenteditable true或嵌入式 3rd party editor written in JavaScript

第三方已做出一些努力 create styled text editors using native JavaFX不依赖于 WebView 或 HTMLEditor 的构造,但截至今天,我认为还没有任何一个已准备好广泛使用。

关于image - 在 JavaFX HTMLeditor 中设置本地镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15802619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com