在JavaFx标签或文本中将文本的一部分加粗 [英] Make portion of a text bold in a JavaFx Label or Text

查看:1070
本文介绍了在JavaFx标签或文本中将文本的一部分加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaFx应用程序中,我需要在整个句子中用粗体字表示一两个字。目前,句子呈现为JavaFx标签,但升级组件也不允许我设置文本,以便我可以将单词Sample以粗体显示。

In my JavaFx application I need to have a word or two rendered in boldface in the whole sentence. Currently the sentence is rendered as a JavaFx Label but upgrading component also would not allow me set the text as so that I can have the words "Sample" displayed in bold.

String s = "This is a <b>Sample</b> sentence"
Label label = new Label(s);

输出

This is a Sample sentence

JavaFx Text也不允许这样做。是否有任何组件可以让我的部分文本以粗体显示?

JavaFx Text also does not allow this. Is there any component where I can have a portion of the text in boldface?

我不确定JavaFx WebView是否适合在窗口中呈现许多小句子。

I am not sure if JavaFx WebView is a good idea for rendering many small sentences in a window.

推荐答案

可以使用JavaFX8中的 TextFlow 容器。
然后你可以在里面轻松添加不同风格的 Text 节点。

It is possible to use TextFlow container from JavaFX8. Then you can easily add differently styled Text nodes inside it.

TextFlow flow = new TextFlow();

Text text1=new Text("Some Text");
text1.setStyle("-fx-font-weight: bold");

Text text2=new Text("Some Text");
text2.setStyle("-fx-font-weight: regular");

flow.getChildren().addAll(text1, text2);

TextFlow容器将自动换行内容文本节点。

TextFlow container will automatically wrap content Text nodes.

这篇关于在JavaFx标签或文本中将文本的一部分加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆