在 Event JavaFX 上创建新的透明舞台

Creating new Transparent Stage on Event JavaFX

我正在尝试在按下按钮时创建一个新的 Stage

它可以工作,但问题是我希望这个 Stage 完全透明,让我们看到屏幕后面的内容。

代码

Dimension Sizescreen = Toolkit.getDefaultToolkit().getScreenSize();

    //Main stage with option menu
    Pane window = new Pane();
    Scene scene = new Scene(window);
    stage.setTitle("Notification Extender");

    //Create the button SetLooker
    Button SetLooker = new Button("Set Looker");
    //Add a Event when pressed
    SetLooker.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {

            //Create a sub-Stage
            Pane subwindow = new Pane();
            Scene subscene = new Scene(subwindow);
            Stage substage = new Stage();
            substage.setTitle("Notification Extender");

            //Set this subStage Transparent
            substage.initStyle(StageStyle.TRANSPARENT);
            subscene.setFill(Color.TRANSPARENT);

            substage.setWidth(Sizescreen.getWidth());
            substage.setHeight(Sizescreen.getHeight());
            substage.setX(0);
            substage.setY(0);

            //Create a a graphique element
            Rectangle redrec = new Rectangle(120,40,50,50);
            redrec.setStroke(Color.RED);
            redrec.setStrokeWidth(2);
            redrec.setFill(Color.TRANSPARENT);

            //Add the graphique element to the sub-stage
            subwindow.getChildren().add(redrec);

            //Show the sub-stage
            substage.setScene(subscene);
            substage.show();

        }
    });

    //Add the button to the main stage
    window.getChildren().add(SetLooker);

    //Show the main stage
    stage.setScene(scene);
    stage.show();

问题是,当我按下按钮时,它显示了舞台,但它根本不透明,它是完全白色的。

我也试过更改主Stage,但一旦显示就无法更改。

您还需要从新场景的根目录中删除背景:

subwindow.setBackground(null);