2

我正在阅读这个问题: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\";"));
}
4

1 回答 1

5

如果您查看DialogPane类,您会看到默认情况下它使用 aLabel作为内容。但你也有这个:

/**
 * Assign dialog content. Any Node can be used
 * 
 * @param content
 *            dialog's content
 */
public final void setContent(Node content) {
    this.content.setValue(content);
}

因此,您需要做的就是为内容提供您的自定义节点:

Text text1=new Text("This is ");
text1.setStyle("-fx-font-size: 16px; -fx-fill: blue;");
Text text2=new Text("underlined ");
text2.setStyle("-fx-font-size: 16px; -fx-fill: blue; -fx-underline: true;");
Text text3=new Text("text");
text3.setStyle("-fx-font-size: 16px; -fx-fill: blue;");

TextFlow flow = new TextFlow(text1,text2,text3);

dialogPane.setContent(flow);

你会有你的对话框:

对话

于 2015-01-10T16:20:08.630 回答