java - JavaFX 中的一段自由流动文本是否可以有一个链接?

标签 java javafx text hyperlink

我正在为一些长文本使用标签来解释有关我的应用程序的内容,并且我希望其中一些单词成为可以触发事物的链接。有没有办法在 JavaFX 上做到这一点?

我知道超链接元素,但这不是我想要的。我只希望部分文本成为链接,而不是全部文本。

最佳答案

一些选项:

  1. TextFlow 中使用 HyperlinkText 元素。这需要做一些工作才能达到生产质量。
  2. 使用RichTextFX ,它允许您通过 CSS 设置各个文本元素的样式,还允许您在鼠标事件中获取字符索引。
  3. 使用 HTML 作为文本并将其显示在 WebView 中。如果您希望链接简单地用作链接,那么这将是开箱即用的;如果您希望更好地控制 JavaFX 应用程序上的事件处理,您还可以使用 Javascript 回调 JavaFX 应用程序。

选项 1 和 3 的演示是:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

public class TextFlowHyperlinkTest extends Application {


    private final static String TEXT = 
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
            + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
            + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
            + "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in "
            + "reprehenderit in voluptate velit esse cillum dolore eu "
            + "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, "
            + "sunt in culpa qui officia deserunt mollit anim id est laborum." ;

    @Override
    public void start(Stage primaryStage) {
        TextFlow textFlow = new TextFlow();

        for (String word : TEXT.split("\\s")) {
            // arbitraily make some words hyperlinks:
            if (word.length() == 6) {
                Hyperlink hyperlink = new Hyperlink(word);
                hyperlink.setOnAction(e -> System.out.println("Click on "+word));
                textFlow.getChildren().add(hyperlink);
            } else {
                textFlow.getChildren().add(new Text(word+" "));
            }
        }

        ScrollPane textFlowScroller = new ScrollPane(textFlow);
        textFlowScroller.setFitToWidth(true);
        textFlowScroller.setMinHeight(200);
        VBox.setVgrow(textFlowScroller, Priority.ALWAYS);

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.documentProperty().addListener((obs, oldDoc, newDoc) -> {
            if (newDoc != null) {
                JSObject window = (JSObject) engine.executeScript("window");
                window.setMember("app", this);
            }
        });
        StringBuilder html = new StringBuilder();
        html.append("<html><body><div name='text'>");
        for (String word : TEXT.split("\\s")) {
            if (word.length() == 6) {
                html.append("<a href='#' onclick='app.process(\""+word+"\")'>")
                    .append(word).append("</a> ");
            } else {
                html.append(word).append(" ");
            }
        }
        html.append("</div></body></html>");
        engine.loadContent(html.toString());

        VBox.setVgrow(webView, Priority.ALWAYS);


        Scene scene = new Scene(new VBox(10, textFlowScroller, webView), 400, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void process(String text) {
        System.out.println("Click on "+text);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

关于java - JavaFX 中的一段自由流动文本是否可以有一个链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48961071/

相关文章:

java - 在从 TextField (FXML) 扩展的自定义类中分配 boolean 值时出错

Linux - 如何将 file1.txt 的全部内容插入到 file2.txt 的第 4 行

html - 从文本 html Canvas 中提取路径

iOS PDF 到纯文本解析器

java - Java 中移位时出现奇怪的结果

java - 无法打开 JDBC 连接 oracle 和 spring boot

java - JavaFX 中的 MouseEvent、ActionEvent 和 Event 有什么区别?

java - PHP Json解析Java对象

java - 创建链表解析行麻烦

swing - 与OpenCV一起使用的JavaFx中的JPanel.getGraphics等效项