我有一个场景(在 SceneBuilder 中创建),其中包含许多标签,我想通过在标签文本开头添加红色星号来将其中一些标签显示为必填字段。我看不到如何在 SceneBuilder 中执行此操作。有没有办法使用 CSS 来做到这一点。
以下是 FXML 文件中的标签片段:
<Label fx:id="lblFirstName" alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="295.0" text="First Name:" GridPane.columnIndex="1" GridPane.rowIndex="2">
<padding>
<Insets right="10.0" />
</padding>
</Label>
到目前为止我发现的所有内容都与 HTML 文件有关。
最佳答案
色拉建议很好,但我建议使用验证的另一种方法。
请注意,此方法需要 ControlsFX 库和 Controller 中的一些代码(不能像您提到的那样在 css 中完成)。
在您的 Controller 类中创建一个验证器:
ValidationSupport validationSupport = new ValidationSupport();
将验证(作为必填字段)添加到初始化方法内的节点(本例中的 TextField):
validationSupport.registerValidator(myTextField, Validator.createEmptyValidator("Field is required"));
关于css - 将红色星号添加到 javafx 标签文本的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60002499/