JavaFX ControlFX对话框中的Action Buttons css样式

我一直在使用ControlsFX对话框来显示信息,但我的应用程序的样式不是蓝色,并且不匹配对话框样式(颜色,边框)有没有办法更改按钮颜色或样式?

注意

在最近的一个问题中 ,这次关于与JDK8u40早期版本捆绑在一起的新Dialog API,我提出了一个更简洁,更干净的解决方案,使用样式表而不是内联样式和查找。

所以我正在更新这个问题,因为openjfx-dialogs 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 openjfx-dialogs.jar (在com.sun.javafx.scene.control.skin.modena包下)查找dialog.css文件,或者转到存储库中的源代码。

现在我们需要提供自定义规则来覆盖dialogalert类选择器的默认规则。 以下规则与我的第一个答案中的内联样式具有完全相同的效果。

 .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"); } 

由于您没有提供您正在使用的版本,我将使用新的OpenJFX-Dialogs项目( 源代码 ,从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 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 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\";")); } 

这就是它的样子:

自定义警报对话框