我在try 更改嵌入在JavaFXAlert中的超链接对象中的文本 colored颜色 时遇到了一个问题(使用ExpandableContent).

此外,我想强调的是,CSS文件被添加到对话框中(事实上,背景 colored颜色 和其他格式被正确识别).

我的CSS class:alert_001

尽管我做了几次try ,但还是未能达到预期的结果. 超链接的文本 colored颜色 在alert 对象中保持不变.

DialogPane (dialog-pane)
+- ButtonBar (button-bar)
+--- HBox (container)
+------ Hyperlink (details-button more)(pseudo class state: visited)
+--------- LabeledText (text)
.alert_001 .dialog-pane .button-bar .container .details-button .more .text {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

提前感谢您

Minimal reproducible example

Create a new Java project (Java 8, no Gradle)

Main class (Directory: src, Package: my.project, File: Main.java)

package my.project;

import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        final AlertComposer alertComposer = new AlertComposer();

        try {

            throw new Exception("Test");

        } catch (Exception e) {

            alertComposer.exception(e);
            throw new RuntimeException(e);

        }
    }
}

Alert composer class (Directory: src, Package: my.project, File: AlertComposer.java)

package my.project;

import javafx.scene.control.Alert;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;

import java.io.PrintWriter;
import java.io.StringWriter;

public class AlertComposer {

    public void exception(Exception e) {

        String content = e.getMessage();
        Alert alert = this.build(Alert.AlertType.ERROR, "myTitle", "myHeader", content);

        GridPane expContent = buildExceptionGridPane(e);

        alert.getDialogPane().setExpandableContent(expContent);
        alert.showAndWait();
    }

    private GridPane buildExceptionGridPane(Exception e) {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String exceptionText = sw.toString();

        TextArea textArea = new TextArea(exceptionText);
        textArea.setEditable(false);
        textArea.setWrapText(true);

        textArea.setMaxWidth(Double.MAX_VALUE);
        textArea.setMaxHeight(Double.MAX_VALUE);
        GridPane.setVgrow(textArea, Priority.ALWAYS);
        GridPane.setHgrow(textArea, Priority.ALWAYS);

        GridPane expContent = new GridPane();
        expContent.setMaxWidth(Double.MAX_VALUE);
        expContent.add(textArea, 0, 0);

        return expContent;
    }

    public Alert build(Alert.AlertType type, String title, String header, String content) {

        Alert alert = build(type);

        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(content);

        return alert;
    }

    private Alert build(Alert.AlertType type) {

        Alert alert = new Alert(type);

        DialogPane dialogPane = alert.getDialogPane();
        dialogPane.setMinHeight(Region.USE_PREF_SIZE);

        dialogPane.getStylesheets().add(Main.class.getResource("/theme.css").toExternalForm());
        dialogPane.getStyleClass().add("alert_001");

        return alert;
    }
}

Theme CSS (Directory: resources, File: theme.css)

.alert_001 {
    -fx-background-color: #282828;
}

.alert_001 .header-panel {
    -fx-background-color: #45494a;
}

.alert_001 .label {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

.alert_001 .button {
    -fx-border-color: #5e6060;
    -fx-border-radius: 10.0 10.0 10.0 10.0;
    -fx-background-color: #3c3f41;
    -fx-background-radius: 10.0 10.0 10.0 10.0;
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 14.0pt;
}

要更改的文本 colored颜色

Alert text color to change

推荐答案

的 Select 器不正确,原因有多种.

一般来说,如果你有两个 Select 器用空格分隔,它将匹配第二个 Select 器的任何 node ,而第二个 Select 器是匹配第一个 Select 器的 node 的后代 node ."后代 node "通常指的是将另一个 node 作为父 node ,或者作为父 node 的父 node ,等等(尽管有一些例外).

所以你的 Select 器

.alert_001 .dialog-pane .button-bar .container .details-button .more .text {
   
}

将匹配具有样式类text的 node ,样式类text是具有样式类more的 node 的后代,样式类details-button是具有样式类container的 node 的后代,等等.

如您所注意到的,more样式类应用于也具有样式类details-button的超链接.因此,没有样式类more的 node 包含在样式类details-button的 node 中(只有一个 node 包含两个样式类).

类似地,您将样式类alert_001添加到对话框窗格中.因此,样式类为alert_001的 node 中没有包含样式类为dialog-pane的 node (只有一个 node ,即对话框窗格,同时包含两个样式类).

还请注意,.text Select 器将 Select 用于显示超链接文本的Text node .Text node 不具有-fx-text-fill属性(参见documentation),但具有继承自Shape-fx-fill属性.

如果要 Select 与两个不同 Select node 相匹配的单个 node ,则可以不使用空格连接 Select 规则.因此,下面将 Select 一个既具有样式类dialog-pane and样式类alert_001的 node :

.alert_001.dialog-pane {
    /* ... */
}

同样, Select 器

.details-button.more {
    /* ... */
}

将 Select 具有样式类details-button和样式类more的 node .

因此,如果你想在CSS中真正具体化,你可以 Select "more"超链接中的文本,

.alert_001.dialog-pane .button-bar .container .details-button.more .text{
    -fx-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

我不认为你需要这么具体(通常更有意义的是设计control(即超链接),而不是其底层渲染).所以我会简单地

.alert_001 .more {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

如果要特别 Select 展开(而不是折叠)按钮,或者

.alert_001 .less {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

设置折叠(但不设置展开)按钮的样式,或

.alert_001 .details-button {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

设置按钮在任一状态下的样式.

另请注意,由于此处的样式与应用于对话框窗格中标签的样式完全相同,因此可以将这些样式合并为一个规则:

.alert_001 .label, .alert_001 .details-button {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

因此,下面的样式表实现了我认为您想要的:

.alert_001 {
    -fx-background-color: #282828;
}

.alert_001 .header-panel {
    -fx-background-color: #45494a;
}

.alert_001 .label, .alert_001 .details-button {
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 12.0pt;
}

.alert_001 .button {
    -fx-border-color: #5e6060;
    -fx-border-radius: 10.0 10.0 10.0 10.0;
    -fx-background-color: #3c3f41;
    -fx-background-radius: 10.0 10.0 10.0 10.0;
    -fx-text-fill: #bbbbbb;
    -fx-font-size: 14.0pt;
}

这里需要注意的是,细节按钮CSS样式类没有文档说明,因此不能特别保证它在future 的JavaFX版本中继续工作.在发布新版本时,Javascript团队似乎非常擅长保留CSS样式名称的向后兼容性(即使是未文档化的功能),而且实际上没有其他方法来获得这些元素.

Java相关问答推荐

Spring Webocket:尽管凭据设置为False,但MLhttpsify和Fetch请求之间的CORS行为存在差异

将偶数元素移动到数组的前面,同时保持相对顺序

Java应用程序崩溃时试图读取联系人从电话

当我用OkHttpClient重写shouldInterceptRequest来发布数据时,Android WebView正在以纯HTML加载URL内容

Oracle DUAL表上使用DDL时jOOQ问题的解析'

尽管类型擦除,instanceof与泛型在Java中如何工作?

如何调用Firebase Realtime Database中的子图像列表到android studio中的回收器视图?

无法初始化JPA实体管理器工厂:无法确定为Java类型<;类>;推荐的JdbcType

查找剩余的枚举

Com.google.firebase.database.DatabaseException:无法将类型为java.lang.Boolean的值转换为字符串.这是关于什么的?

我如何知道MediaDiscoverer何时完成发现介质?

MySQL数据库中未应用具有Spring数据的唯一约束

从ActiveMQ Classic迁移到ActiveMQ Artemis需要进行哪些客户端更改?

尽管通过中断请求线程死亡,但线程仍将继续存在

使用用户引入的参数生成人员数组

Domino Designer 14中的保存代理添加了重影库

在学习Spring时,通过构造函数参数0表达了不满意的依赖关系

如何创建模块信息类文件并将其添加到JAR中?

如果第一位数字和最后一位数字相差超过一位,您将如何获得随机数?

如何使用我的RLE程序解决此问题