有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文本字段,用于在JavaFX聊天应用程序中输入文本和插入图像

我正在JavaFX上开发一个聊天应用程序,我遇到了一个小问题。简言之,我想有一个可能性,以插入一个图像(例如微笑)内的TextField是为文本输入。但正如我所知,在这个元素中插入图像是不可能的。JavaFX是否有一些替代方案,或者您可能知道其他一些东西,我可以使用什么

提前谢谢你


共 (1) 个答案

  1. # 1 楼答案

    您可以尝试使用HTMLEditor。它支持样式文本和其他html功能,包括添加图像。 有一个使用HTMLEditor作为聊天输入字段的示例:

    package org.laptech.sample;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.web.HTMLEditor;
    import javafx.stage.Stage;
    
    
        public class ChatFieldSample extends Application{
    
        private HTMLEditor htmlEditor;
    
        public void hideHTMLEditorToolbars() {
            htmlEditor.setVisible(false);
            Platform.runLater(() -> {
                Node[] nodes = htmlEditor.lookupAll(".tool-bar").toArray(new Node[0]);
                for (Node node : nodes) {
                    node.setVisible(false);
                    node.setManaged(false);
                }
                htmlEditor.setVisible(true);
            });
        }
    
    
        @Override public void start(Stage primaryStage) throws Exception {
            htmlEditor = new HTMLEditor();
            hideHTMLEditorToolbars();
            htmlEditor.setHtmlText("Text<img src='urltosmile'/>Text Text Text");
            primaryStage.setScene(new Scene(htmlEditor, 300, 300));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    我创建htmlEditor并在方法中隐藏工具栏(HideHtmlEditor工具栏) 我唯一面临的问题是,当我处理keyEvents和setText时,会失去焦点