简体   繁体   English

在JavaFX Openjfx对话框项目中设置对话框样式

[英]Styling a dialog from JavaFX Openjfx Dialogs project

I was reading this question: Action Buttons css style in JavaFX ControlFX dialog , to find out whether it's possible to change JavaFX alert dialogs in terms of style. 我在读这个问题: JavaFX ControlFX对话框中的Action Buttons css样式 ,以了解是否有可能在样式方面更改JavaFX警报对话框。

The answer was pretty good but I was wondering whether it's possible to just format certain words in the dialog? 答案非常好,但是我想知道是否可以仅在对话框中设置某些单词的格式?

For example, I just want one word to be underlined, I can't figure out how to do so though, because there is just one .content.label and the JavaFX Text class does not work properly with dialogs (if I'm not wrong). 例如,我只想给一个单词加下划线,但是我不知道该怎么做,因为只有一个.content.label ,而JavaFX Text类不能与对话框一起正常使用(如果我不错误)。

Below is the code piece how the entire Alert dialog got modified. 下面的代码段是如何修改整个“ 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\";"));
}

If you have a look at DialogPane class, you'll see that by default it uses a Label for the content. 如果您查看DialogPane类,您会发现默认情况下它使用Label作为内容。 But also you have this: 但是你也有这个:

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

So all you need to do is provide your custom node for the 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);

and you'll have your dialog: 您将看到对话框:

对话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM