JavaFX ControlFX对话框中的Action Buttons css样式 [英] Action Buttons css style in JavaFX ControlFX dialog

查看:620
本文介绍了JavaFX ControlFX对话框中的Action Buttons css样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been using ControlsFX dialogs to show information, but the style of my application is not blue, and does not match dialog style (color, borders) is there a way to change the button color or styles?

推荐答案

注意

在最近的< a href =https://stackoverflow.com/questions/28417140/styling-default-javafx-dialogs>问题,这次关于新的对话与JDK8u40早期版本捆绑在一起的API,我带来了一个不那么hacky和更干净的解决方案,使用样式表而不是内联样式和查找。

In a more recent question, this time regarding the new Dialog API bundled with JDK8u40 early releases, I came with a less hacky and more clean solution, using stylesheets instead of inline styles and lookups.

所以我正在更新这个问题,因为 openjfx-dialogs 仍然是官方对话的方式发布8u20,8u25和8u31。

So I'm updating this question, as openjfx-dialogs is still the way to have dialogs for the official releases 8u20, 8u25 and 8u31.

新解决方案

自定义默认值使用我们自己的css文件的对话框的样式,我们需要考虑到对话框实际上是一个新的阶段,有一个新的场景,根节点是 DialogPane 实例。

To customize the default style of a dialog with our own css file, we need to take into consideration that the dialog is in fact a new stage, with a new scene, and the root node is a DialogPane instance.

因此,一旦我们有一些对话框实例:

So once we have some dialog instance:

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

我们可以访问其对话窗格并添加我们自己的样式表:

we can access to its dialog pane and add our own style sheet:

DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(
   getClass().getResource("myDialogs.css").toExternalForm());

为了定义我们的规则,我们需要知道已经使用过的描述符。为此,我们只需要在 openjfx-dialogs.jar 中找到 dialog.css 文件(在包下面) com.sun.javafx.scene.control.skin.modena),或转到存储库的源代码

In order to define our rules we need to know the descriptors already used. For that, we just need to look for dialog.css file in the openjfx-dialogs.jar (under the package com.sun.javafx.scene.control.skin.modena), or go to the source code at the repository.

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

Now we need to provide our custom rules to override the default ones for dialog and alert class selectors. The following rules will have the exact same effect as the inline styiling from my first answer.

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

这篇关于JavaFX ControlFX对话框中的Action Buttons css样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆