如何删除对话框的背景?

How to remove the background of a dialog?

我创建了一个自定义对话框,其中包含我自己的窗格和控件。但是对话框默认有一个白色边框,我想将其删除。这是一个单张图片的例子:

我尝试使用 ScenicView,但找不到捕捉对话层并对其进行修改的方法:

public class MainView extends View {

    Image img = new Image("https://i.stack.imgur.com/7bI1Y.jpg", 300, 500, true, true);

    public MainView(String name) {
        super(name);

        Button b = new Button("Pop");
        b.setOnAction(e -> {
            Dialog<Void> dialog = new Dialog<>();
            dialog.setOnShown(e2 -> {
                Parent parent = getParent();
                Pane p = (Pane) parent.lookup(".dialog");
                p.setPadding(new Insets(0));
            });
            dialog.setGraphic(new ImageView(img));
            dialog.showAndWait();
        });
        setCenter(b);
    }
}

我得到的最好的结果是移除流动窗格子以移除一些下部

dialog.setOnShown(e2 -> {
    Parent parent = getParent();
    Pane p = (Pane) parent.lookup(".dialog");
    p.getChildren().removeIf(c -> (c instanceof FlowPane));
    System.out.println(p.getChildren());
});

删除 VBox 会移动我不想做的对话框,更改它的填充也不会做任何事情。

正如您在 ScenicView 中看到的那样,Dialog 具有 dialog 样式 class。

修改对话框样式的一种简单方法是通过 css。只需将 css 文件添加到您的视图,然后设置:

.dialog {
    -fx-background-color: transparent;
}

这会将背景设置为透明,而不是默认的白色。

如果您想改为删除边框,则可以使用填充。正如您还可以在 ScenicView 中看到的那样,该对话框的中心内容有一个样式为 class container 的 VBox,底部的按钮有一个样式为 class 的流程窗格dialog-button-bar

首先,只需使用 setContent 方法添加图像而不是 setGraphic 方法:

dialog.setContent(new ImageView(img));

这将需要删除所有边框,并让图像占据整个对话框:

.dialog,
.dialog > .container,
.dialog > .dialog-button-bar {
    -fx-padding: 0;
}