要禁用JavaFX DatePicker的TextField,可以使用以下方法:
.date-picker-text-field {
-fx-opacity: 1;
-fx-background-color: #f4f4f4;
-fx-text-fill: #888888;
-fx-background-insets: 0;
-fx-background-radius: 0;
-fx-border-color: transparent;
-fx-border-width: 0;
-fx-prompt-text-fill: #888888;
-fx-padding: 0;
-fx-cursor: default;
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
-fx-font-size: 12px;
-fx-font-family: "Arial";
}
.date-picker-text-field:disabled {
-fx-opacity: 1;
-fx-background-color: #f4f4f4;
-fx-text-fill: #888888;
-fx-background-insets: 0;
-fx-background-radius: 0;
-fx-border-color: transparent;
-fx-border-width: 0;
-fx-prompt-text-fill: #888888;
-fx-padding: 0;
-fx-cursor: default;
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
-fx-font-size: 12px;
-fx-font-family: "Arial";
}
然后,在Java代码中,为DatePicker的TextField添加该样式类:
datePicker.getStyleClass().add("date-picker-text-field");
datePicker.setEditable(false);
这样,用户将无法编辑或选择DatePicker的TextField。
请注意,以上方法仅禁用了DatePicker的TextField部分,而不是整个DatePicker组件。
领取专属 10元无门槛券
手把手带您无忧上云