【问题标题】:JavaFX ChoiceDialog CSSJavaFX 选择对话框 CSS
【发布时间】:2017-07-18 16:26:09
【问题描述】:

我的 JavaFX 应用程序中有一个 ChoiceDialog,我一生都无法弄清楚需要哪些 CSS 元素来改变它的外观和感觉。我的其他对话框窗格似乎受到我的外部 CSS 文件的影响,所以我猜这是我不熟悉的元素。

另外,这将覆盖 modena css。

我试过了:

.dialog-pane {
-fx-background-color: black;
}

.dialog-pane .label {
-fx-text-fill: white;
}

.dialog-pane:header .header-panel {
-fx-background-color: black;
}

.dialog-pane:header .header-panel .label {
-fx-font-style: italic;
-fx-font-size: 2em;
}

我也试过了:

.choice-dialog .dialog-pane {
-fx-background-color: black;
}

.choice-dialog .dialog-pane .label {
-fx-text-fill: white;
}

.choice-dialog .dialog-pane:header .header-panel {
-fx-background-color: black;
}

.choice-dialog .dialog-pane:header .header-panel .label {
-fx-font-style: italic;
-fx-font-size: 2em;
}

以及这 2 个的其他一些变体(.choice-dialog 和 .dialog-pane) 我尝试的另一件事(查看 modena.css 文件后)是通过执行以下操作来更改 ChoiceDialog 中显示的图标:

来自

.choice-dialog.dialog-pane {
-fx-graphic: url("dialog-confirm.png");
}

.choice-dialog.dialog-pane {
-fx-graphic: url("dialog-warning.png");
}

虽然这也没有产生任何结果。

更新

我更新了我的自定义 CSS 文件,将其全部删除并重试了上述 CSS。 CSS 的第一个块仅更改 DialogPane,而不是 ChoiceDialog。第二个和第三个什么都没做。

这是我用来创建 ChoiceDialog 的代码

ChoiceDialog<String> dialog = new ChoiceDialog<>("district", choices);
dialog.setTitle("Object Selection");
dialog.setHeaderText("Which object should the file inherit from?");
dialog.setContentText("Default Object:");

Stage dialogStage = (Stage) dialog.getDialogPane().getScene().getWindow();
dialogStage.getIcons().add(icon);
dialogStage.initOwner(stage);

Optional<String> response = dialog.showAndWait();
response.ifPresent(chosen ->
{
    //It does something...
});

Dialog<ObservableList<DataFilter>> dialog = new Dialog<>();
dialog.getDialogPane().setPrefSize(620, 430);
dialog.setTitle("Field Filter");
dialog.getDialogPane().getButtonTypes().addAll(save_bt, cancel_bt);
dialog.initOwner(stage);
dialog.setResultConverter((ButtonType b) ->
{
    if (b == save_bt)
    {
        return FXCollections.observableArrayList(dataFilters);
    }
    return null;
 });

 Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
 stage.getIcons().add(icon);

【问题讨论】:

  • 你能提供更多细节吗?你想应用什么风格,为了应用它你实际尝试了什么?发布minimal reproducible example
  • 已更新以显示我尝试过的内容。
  • 您是否尝试过.choice-dialog.dialog-pane(如您从modena.css 发布的sn-p,没有空格),而不是.choice-dialog .dialog-pane(有空格)? url(...) 函数将查找相对于 CSS 文件的图像文件,因此除非你有一个,否则 sn-p 将不起作用。如果这些修复不起作用,我建议您发布minimal reproducible example
  • 你的第一个代码块改变了背景颜色和文本填充对我来说很好。
  • 我再次更新并添加了一些 Java 和图像以帮助按照建议进行描述。

标签: css javafx-8


【解决方案1】:

花了我一个小时的实验。其他几个选择器位于 modena.css 中的“基于默认列表视图的组合框的样式”下

.dialog-pane{
-fx-background-color: #7160DC;
   }
 .dialog-pane > *.button-bar > *.container{
 -fx-background-color: #7160DC;
  }
  .dialog-pane > .content .label{
  -fx-font-size: 15px;
  -fx-font-weight: bold;
   -fx-text-fill: orange;
   }
  .dialog-pane:header .header-panel{
   -fx-background-color: #9589DF;
    }
    .dialog-pane:header .header-panel .label{
    -fx-font-size: 18px;
    -fx-font-style: italic;
     }

   .button{
    -fx-background-color:black;
    -fx-text-fill:white;
    }

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

   /* styles background of cover choice*/
   .combo-box {
  -fx-background-color: black;
  -fx-font-weight: bold;
   }
  /* styles text of cover choice*/
  .combo-box > .list-cell {
  -fx-text-fill: yellow;
  }

 .combo-box-popup > .list-view {
-fx-background-color:black;
-fx-background-insets: 0, 1;
-fx-effect: dropshadow( gaussian , rgba(0,0,0,0.2) , 12, 0.0 , 0 , 8 );
 }

.combo-box-popup > .list-view > .virtual-flow > .clipped-container > .sheet >  .list-cell {
-fx-padding: 4 0 4 5;
-fx-background: black;
}

.combo-box-popup > .list-view > .virtual-flow > .clipped-container > .sheet   
> .list-cell:filled:hover {
-fx-background-color: orange;
 -fx-text-fill: black;
 }

 //add the above .css file as stylesheet

ChoiceDialog dialog = new ChoiceDialog();
    dialog.setTitle("Confirmation");
    dialog.setContentText("Some Text");
    DialogPane dp = dialog.getDialogPane();
    dp.getStylesheets().add(getClass().getResource("myCssFile.css")
    .toExternalForm());
    Optional<String> result = dialog.showAndWait(); 

【讨论】:

  • 那是什么?它如何回答这个问题?它有什么作用? stackoverflow.com/help/how-to-answer
  • 这是一个 .css 文件并回答了“如何设置 ChoiceDialog 的样式”这个问题?在将其作为样式表添加到 ChoiceDialog 后,它会进行样式设置。我将使用如何添加样式表的代码更新答案。
  • 不是我的意思。我的观点是你不应该在没有解释的情况下脱口而出。没有人能从中吸取教训。
猜你喜欢
  • 2014-05-08
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多