我在尝试更改 JavaFX 警报(带有 ExpandableContent)中嵌入的超链接对象内的文本颜色时遇到问题。
此外,我想强调的是,CSS 文件已添加到 DialogPane(事实上,背景颜色和其他格式都可以正确识别)。
我的 CSS 类:
alert_001
尽管多次尝试,我仍无法达到预期的结果。 超链接的文本颜色在警报对象内保持不变。
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;
}
提前谢谢您
src
,包:my.project
,文件: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);
}
}
}
src
,包:my.project
,文件: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;
}
}
resources
,文件: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;
}