我有一个代表表单字段的自定义组件。它具有标签,文本字段和错误消息,输入验证后可能会显示该消息。
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="109.0" prefWidth="512.0" spacing="10.0" styleclass="vbox" type="VBox" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="fieldLabel" text="Lorem ipsum dolor sit amet"></Label>
<TextField fx:id="textField" promptText="Lorem ipsum dolor sit amet"></TextField>
<Label fx:id="errorLabel" text=""></Label>
</children>
</fx:root>
public class FormField extends VBox {
@FXML private TextField textField;
@FXML private Label fieldLabel;
@FXML private Label errorLabel;
private String fieldLabelText;
private String promptText;
public FormField(@NamedArg("fieldLabelText") String fieldLabelText,@NamedArg("promptText") String promptText,@NamedArg("isPasswordField") boolean isPasswordField) {
FXMLLoader loader = new FXMLLoader(getclass().getResource("../../resources/fxml/form-field.fxml"));
loader.setRoot(this);
loader.setController(this);
this.fieldLabelText = fieldLabelText;
this.promptText = promptText;
try {
loader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
@FXML
public void initialize() {
this.fieldLabel.setText(fieldLabelText);
this.textField.setPromptText(promptText);
}
现在我想知道的是如何对具有PasswordField
而不是TextField
的组件进行扩展?还是传递诸如boolean isPasswordField
之类的参数,让FormField
决定应呈现TextField
还是PasswordField
?如果TextField
的API中有一个obscureText(true)
方法,那就足够了,因为这就是我要寻找的全部,但是我找不到任何东西。
我能找到的关于JavaFX继承的全部含义是通过向对象添加新组件而不是通过更改其现有元素来“扩展”对象。