【发布时间】:2020-09-22 19:19:29
【问题描述】:
我尝试实施一些现有的解决方案,但对我没有帮助。 从 API 接收到一个字符串“您可以访问网站:http://localhost:0000/stack_overflow/”。 需要显示整个字符串。 我想以这样一种方式拆分字符串,使“http://localhost:0000/stack_overflow/”部分成为超链接并可点击。
我尝试使用 TextFlow 并将文本拆分为文本和超链接,但是,当我使用“:”进行拆分时,所有的“:”都会破坏字符串。
String urlLink = "You can visit out website:http://localhost:0000/stack_overflow/";
TextFlow textFlow = new TextFlow();
ImageView imageView = new ImageView();
imageView.setImage(new Image(Resources.ICON));
String[] information = urlLink.split(":");
Text txtInfo = new Text(information[0]);
Hyperlink link = new Hyperlink(information[1]);
link.setOnAction(event -> {
try {
Runtime.getRuntime().exec("cmd.exe /c start iexplore " + link);
} catch (IOException e) {
e.printStackTrace();
}
});
textFlow = new TextFlow(txtInfo, link);
【问题讨论】:
-
收到的字符串有时可能没有空格。而且当我拆分时,我仍然希望“:”在显示屏上可见。
-
因此,您代码中的所有 JavaFX 内容都与您的问题无关,即如何在正确的位置
split一个字符串。代码示例甚至不一致,因为它以声明名为urlLink的变量开始,但随后突然使用description.getCaption()代替变量。一个简单的解决方案是urlLink.split("(?=https?:)");,在http:或https:url 开头之前进行拆分。
标签: java javafx java-8 hyperlink