Use 获取资源 http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29获取本地图片资源的位置。
editor.setHtmlText(
"<img src=\"" +
getClass().getResource("categoryButton.fw.png") +
"\" width=\"32\" height=\"32\" >"
);
它的工作方式与将内容加载到相同WebView http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebView.html as in:
如何从 javafx.scene.web.WebEngine#loadContent 加载的 html 页面访问 css 和图像文件? https://stackoverflow.com/questions/8923801/how-to-reach-css-and-image-files-from-the-html-page-loaded-by-javafx-scene-web-w
这是一个屏幕截图:
以及一个可执行示例:
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); }
}
我只是好奇这是否是将图像放入某种文本区域的唯一方法?
- 使用 JavaFX 2,您可以将与文本混合的图像嵌入到FlowPane http://docs.oracle.com/javafx/2/api/javafx/scene/layout/FlowPane.html如中所述Javafx Text 多字着色 https://stackoverflow.com/questions/15081892/javafx-text-multi-word-colorization.
- Java 8 添加了一个TextFlow https://wikis.oracle.com/display/OpenJDK/Rich+Text+API+Samples允许您将图像嵌入到文本区域的组件。
以上两种技术仅用于数据显示。两者都不允许编辑插入图像和其他节点的多样式文本。目前,JavaFX 平台为此功能提供的唯一控件是HTML 编辑器 http://docs.oracle.com/javafx/2/api/javafx/scene/web/HTMLEditor.html or a WebView http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebView.html with 内容可编辑 true https://stackoverflow.com/questions/10685395/webview-with-contenteditable-cannot-be-focused-programmatically或嵌入式用 JavaScript 编写的第 3 方编辑器 https://gist.github.com/jewelsea/1463485.
第三方已经做出了一些努力使用本机 JavaFX 创建样式文本编辑器 http://tomsondev.bestsolution.at/2013/02/20/from-styledtextviewer-to-a-styledtexteditor-with-javafx8/不依赖于 WebView 或 HTMLEditor 的构造,但截至今天,我认为还没有任何一个已准备好广泛使用。