如何使JavaFX后台透明(仅后台)

如何使JavaFX后台透明(仅后台),javafx,transparent,Javafx,Transparent,下面的示例仅适用于文本,但一旦我在舞台上添加了一个按钮,透明屏幕将变为非活动状态 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.Text; import javaf

下面的示例仅适用于文本,但一旦我在舞台上添加了一个按钮,透明屏幕将变为非活动状态

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.initStyle(StageStyle.TRANSPARENT);
        Text text = new Text("!");
        text.setFont(new Font(40));
        VBox box = new VBox();
        Button btn = new Button("Test transparent");
        box.getChildren().addAll(text, btn);
        //if I removed the btn, transparent works as expected. 
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我想要的是只让舞台透明,但显示文本和按钮

你的
按钮
,这样就不成问题了。默认情况下,
VBox
具有灰色背景,因此您的
Stage
是透明的,但VBox不是。您必须通过CSS文件或内联或从代码设置透明背景:

CSS:

内联:

box.setStyle("-fx-background-color: transparent;");
代码:


非常感谢你!!
box.setStyle("-fx-background-color: transparent;");
box.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));