JavaFX 未修饰 Window 带阴影

JavaFX undecorated Window with shadow

我想知道如何创建一个未修饰的 window 及其常规阴影行为(就像使用 Windows 时所有 windows 一样)。

我读了几篇文章,但没有一篇真正奏效。

据我所知,我必须使用某种填充来创建主舞台。这个阶段必须有一个透明的背景。实际内容需要放在其他节点上。

所以我为我的根元素取了一个堆栈窗格,并在上面放置了一个 vbox。 vbox 应该在实际的主舞台区域(我将该区域涂成绿色)。

但我尝试将 StageStyle 用于透明,我尝试用透明背景填充堆栈窗格,但没有一个起作用。阴影也没有按预期工作(我在示例中删除了阴影实验)。

private void createPopup2() {
        StackPane stackePane = new StackPane();
        VBox rootPane = new VBox();
        rootPane.setStyle("-fx-background-color: green; -fx-border-color: black; -fx-border-width: 1px;");
        stackePane.getChildren().add(rootPane);
        stackePane.setPadding(new Insets(20, 20, 20, 20));
        Scene scene = new Scene(stackePane);
        final Stage stage = new Stage();
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setWidth(600);
        stage.setHeight(350);
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
    }

我很困惑,不知道如何解决。

首先我不确定你期待的是什么类型的影子。如果您能提供您尝试过的影子示例,将对我们有所帮助,以便我们了解实际问题。

话虽如此,您是否厌倦了在 VBox 上使用 -fx-effect。?下面的代码在绿色框周围创建阴影效果。

StackPane stackePane = new StackPane();
stackePane.setStyle("-fx-background-color:transparent");
VBox rootPane = new VBox();
rootPane.setStyle("-fx-background-color: green; -fx-border-color: black; -fx-border-width: 1px;-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, .75), 20, 0.19, 0, 0);");
stackePane.getChildren().add(rootPane);
stackePane.setPadding(new Insets(20, 20, 20, 20));
Scene scene = new Scene(stackePane, Color.TRANSPARENT);
final Stage stg = new Stage();
stg.initStyle(StageStyle.TRANSPARENT);
stg.setWidth(600);
stg.setHeight(350);
stg.setScene(scene);
stg.initModality(Modality.APPLICATION_MODAL);
stg.show();

您可以调整阴影的参数以获得您想要的效果。 参数相关文档如下: