JavaFx:自定义警报对话框

时间:2020-09-17 16:11:20

标签: css javafx dialog

我想在“警报”对话框中自定义按钮,按钮容器,背景色,AlertType图标。

尝试了以下两种解决方案:

  1. Styling default JavaFX Dialogs
  2. Customize JavaFx Alert with css

我想我提到的CSS代码应该适用于所有Alert对话框? 不知道我在这里想念什么。

private static void createSimpleInformationDialog(String message){
    Alert alert = createSimpleInformationAlert(message, AlertType.INFORMATION);
    alert.getDialogPane().setHeaderText(StringTools.isNull(null, ""));
    alert.getDialogPane().setMaxWidth(200);
    alert.getDialogPane().setMinWidth(150);
    alert.getDialogPane().setPadding(new Insets(0, 10, 0, 10));
    alert.showAndWait();
}

private static Alert createSimpleInformationAlert(String message, AlertType type) {
    Alert alert = new Alert(type);
    alert.setTitle(Lang.get(Defs.FX_DIALOGS_EXCEPTIONS_GENERIC_TITLE));
    alert.setContentText(message);
    alert.initModality(Modality.APPLICATION_MODAL);
    alert.initOwner(FXMain.getInstance().getStage());
    return alert;
}

CSS文件:

.dialog-pane{
  -fx-border-color:black;
  -fx-border-width:2.0px;
 }

/**Costumization of The Bar where the buttons are located**/
.dialog-pane > .button-bar > .container {
  -fx-background-color:black;
}

.dialog-pane > .content.label {
   -fx-padding: 0.5em 0.5em 0.5em 0.5em;
   -fx-background-color: yellow;
   -fx-text-fill:black;
   -fx-font-size:15.0px;
}

/**Costumization of DialogPane Header**/
.dialog-pane:header .header-panel {
  -fx-background-color: black;
}

.dialog-pane:header .header-panel .label{
  -fx-background-color: yellow;
  -fx-background-radius:10px;
  -fx-text-fill:black;
  -fx-font-size:15.0px;
}


/**Costumization of Buttons**/
.dialog-pane .button{
   -fx-background-color:black;
   -fx-text-fill:white;
   -fx-wrap-text: true;
   -fx-effect: dropshadow( three-pass-box, yellow, 10.0, 0.0, 0.0, 0.0);
   -fx-cursor:hand;
 }

.dialog-pane .button:hover{     
  -fx-background-color:white;
  -fx-text-fill:black;
  -fx-font-weight:bold; 
 }

0 个答案:

没有答案