在 JavaFx 标签或文本中将文本的一部分设为粗体 [英] Make portion of a text bold in a JavaFx Label or Text
问题描述
在我的 JavaFx 应用程序中,我需要在整个句子中用粗体显示一两个单词.目前,该句子呈现为 JavaFx 标签,但升级组件也不允许我将文本设置为粗体显示示例"字样.
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屋!