我正在阅读这个问题:JavaFX ControlFX 对话框中的操作按钮 css 样式,以了解是否可以根据样式更改 JavaFX 警报对话框。
答案非常好,但我想知道是否可以在对话框中格式化某些单词?
例如,我只想给一个词加下划线,但我不知道该怎么做,因为只有一个词.content.label
,而且 JavaFXText
类不能与对话框一起正常工作(如果我没记错的话)。
下面是如何修改整个Alert
对话框的代码片段。
@Override
public void start(Stage primaryStage) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("This is a Custom Confirmation Dialog");
alert.setContentText("We override the style classes of dialog.css");
Button button = new Button("Click to display an alert");
button.setOnAction(e->{
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(System.out::println);
});
Scene scene = new Scene(new StackPane(button), 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
DialogPane dialogPane = alert.getDialogPane();
dialogPane.setStyle("-fx-background-color: greenyellow;");
dialogPane.getStyleClass().remove("alert");
GridPane grid = (GridPane)dialogPane.lookup(".header-panel");
grid.setStyle("-fx-background-color: cadetblue; "
+ "-fx-font-style: italic;");
StackPane stackPane = new StackPane(new ImageView(
new Image(getClass().getResourceAsStream("lock24.png"))));
stackPane.setPrefSize(24, 24);
stackPane.setAlignment(Pos.CENTER);
dialogPane.setGraphic(stackPane);
dialogPane.lookup(".content.label").setStyle("-fx-font-size: 16px; "
+ "-fx-font-weight: bold; -fx-fill: blue;");
ButtonBar buttonBar = (ButtonBar)alert.getDialogPane().lookup(".button-bar");
buttonBar.setStyle("-fx-font-size: 24px;"
+ "-fx-background-color: indianred;");
buttonBar.getButtons().forEach(b->b.setStyle("-fx-font-family: \"Andalus\";"));
}