Joh*_*lez 3 java user-interface javafx controlsfx
我一直在使用ControlsFX对话框来显示信息,但我的应用程序的样式不是蓝色,并且不匹配对话框样式(颜色,边框)有没有办法更改按钮颜色或样式?
既然你不提供你正在使用的版本,我会用新的OpenJFX的对话框项目去(源,因为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<ButtonType> result = alert.showAndWait();
result.ifPresent(System.out::println);
});
Scene scene = new Scene(new StackPane(button), 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)
现在,为了设置此对话框的样式,我们需要一个实例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<ButtonType> 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\";"));
}
Run Code Online (Sandbox Code Playgroud)
这就是它的样子: