我在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;
}