我一直在使用 ControlsFX 对话框来显示信息,但是我的应用程序的样式不是蓝色的,并且与对话框样式(颜色、边框)不匹配 有没有办法更改按钮颜色或样式?
3 回答
由于您没有提供您正在使用的版本,我将使用新的 OpenJFX-Dialogs 项目(source,从 8.20.7版本开始),顺便说一下,我们将使用 JDK8u40 使用相同的对话框。
首先,让我们添加一些警报对话框:
@Override
public void start(Stage primaryStage) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("This is a Regular Confirmation Dialog");
alert.setContentText("This is the message");
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
,它是对话框中的根节点,其中显示了标题、内容和按钮。
通过查找或通过getChildren()
查找这些组件很容易。
这是自定义对话框所有组件的示例:
@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();
// root
dialogPane.setStyle("-fx-background-color: greenyellow;");
// 1. Grid
// remove style to customize header
dialogPane.getStyleClass().remove("alert");
GridPane grid = (GridPane)dialogPane.lookup(".header-panel");
grid.setStyle("-fx-background-color: cadetblue; "
+ "-fx-font-style: italic;");
// custom icon
StackPane stackPane = new StackPane(new ImageView(
new Image(getClass().getResourceAsStream("lock24.png"))));
stackPane.setPrefSize(24, 24);
stackPane.setAlignment(Pos.CENTER);
dialogPane.setGraphic(stackPane);
// 2. ContentText with just a Label
dialogPane.lookup(".content.label").setStyle("-fx-font-size: 16px; "
+ "-fx-font-weight: bold; -fx-fill: blue;");
// 3- ButtonBar
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\";"));
}
这就是它的样子:
笔记
在最近的一个问题中,这次是关于Dialog
与 JDK8u40 早期版本捆绑的新 API,我提出了一个不那么 hacky 和更干净的解决方案,使用样式表而不是内联样式和查找。
所以我正在更新这个问题,因为openjfx-dialogs
仍然是官方版本 8u20、8u25 和 8u31 的对话方式。
新解决方案
要使用我们自己的 css 文件自定义对话框的默认样式,我们需要考虑到对话框实际上是一个新阶段,具有一个新场景,并且根节点是一个DialogPane
实例。
所以一旦我们有了一些对话框实例:
@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 the dialog");
...
}
我们可以访问它的对话框并添加我们自己的样式表:
DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(
getClass().getResource("myDialogs.css").toExternalForm());
为了定义我们的规则,我们需要知道已经使用的描述符。为此,我们只需要dialog.css
在(com.sun.javafx.scene.control.skin.modena 包下)中查找文件openjfx-dialogs.jar
,或者转到存储库中的源代码。
现在我们需要提供我们的自定义规则来覆盖dialog
和alert
类选择器的默认规则。以下规则与我的第一个答案中的内联样式具有完全相同的效果。
.dialog > .dialog-pane {
-fx-background-color: greenyellow;
}
.dialog > .dialog-pane > .button-bar {
-fx-font-size: 24px;
-fx-background-color: indianred;
-fx-font-family: "Andalus";
}
.dialog > .dialog-pane > .content.label {
-fx-font-size: 16px;
-fx-font-weight: bold;
-fx-fill: blue;
}
.dialog:header > .dialog-pane .header-panel {
-fx-background-color: cadetblue;
-fx-font-style: italic;
}
.alert.confirmation.dialog-pane {
-fx-graphic: url("lock24.png");
}
Another solution:
dialog.getDialogPane().getScene().getStylesheets().add("css_name.css");