在 Swing 中,使用 HTML 设置 JLabel 的样式非常简单——您只需使用所需的 HTML 作为标签的文本,然后适本地呈现它。
在 JavaFX 中,这不可用,但我们可以使用 setStyle()
设置特定标签(或一般节点)的样式。方法。
但是,使用这种方法,如何将标签的一部分设置为某种样式并不明显,例如相当于:
JLabel label = new JLabel("<html>Part of this <b>text is b</b>old and part isn't.</html>");
实现上述目标的最简单方法是什么?
最佳答案
您可以尝试使用 TextFlow 来组合不同样式的文本节点,例如
TextFlow textFlow = new TextFlow();
Text first=new Text("Part of this ");
first.setStyle("-fx-font-weight: regular");
Text second=new Text("text is b");
second.setStyle("-fx-font-weight: bold");
Text third=new Text("old and part isn't.");
third.setStyle("-fx-font-weight: regular");
textFlow.getChildren().addAll(first, second, third);
关于styles - JavaFX HTML 样式(或等效)标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12694043/