如何在javafx中制作透明的场景和舞台?

如何在javafx中制作透明的场景和舞台?,javafx,transparent,scene,stage,Javafx,Transparent,Scene,Stage,我想要透明的progressindicator,它是不确定的 这是代码,它显示灰色背景状态/场景。 我想要完全透明 我尝试了以下代码,但它显示的后台阶段是不透明的 package application; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ProgressIndicator; import javafx.scene.layout.VBox;

我想要透明的progressindicator,它是不确定的

这是代码,它显示灰色背景状态/场景。 我想要完全透明

我尝试了以下代码,但它显示的后台阶段是不透明的

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

    public class Main extends Application {

        @Override
        public void start(Stage stage) {
            /*
             * 
             * my css file content:
             * 
             * .progress-indicator .indicator { -fx-background-color: transparent;
             * -fx-background-insets: 0; -fx-background-radius: 0;
             * 
             * } .progress-indicator { -fx-progress-color: green ; }
             * 
             * 
             * 
             */
            Stage initStage = new Stage();

            initStage.initStyle(StageStyle.TRANSPARENT);
            ProgressIndicator loadProgress = new ProgressIndicator();
            loadProgress.setSkin(null);
            loadProgress.setPrefWidth(50);
            VBox box = new VBox();
            box.getChildren().add(loadProgress);
            final Scene scene = new Scene(box, 150, 150);

            scene.setFill(Color.TRANSPARENT);

            initStage.setScene(scene);
            scene.getStylesheets().add("application.css");

            initStage.show();

        }

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

    }

对于
modena.css
(Java8中默认的JavaFX外观定义),所有控件(以及加载控件时的窗格)都引入了轻微的阴影背景

可以通过指定默认背景为透明来删除此选项。这可以通过在应用程序的CSS文件中添加以下行来实现:

.root { -fx-background-color: transparent; }
这是除了代码中已有的用于初始化舞台样式和场景背景填充的其他设置之外的其他设置

stage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(Color.TRANSPARENT);
注意:在问题的示例代码中,创建了一个附加阶段(initStage),而不是使用传入阶段作为start方法。传入阶段可以由代码直接初始化、使用和显示,而不是创建额外的initStage

stage.initStyle(StageStyle.TRANSPARENT);
用于隐藏顶部栏(最小化、向下恢复和关闭)

这是框架颜色(你可以用任何颜色替换透明绿黄红蓝…),但对我来说,如果你能理解我,我想要玻璃视图,并使用不同的颜色,因此解决方案是

primaryStage.setOpacity(0.2);
数字0.2介于0和1之间。0是隐藏的,1是正常形式,但在数字之间是透明的,所以选择你的数字并运行你的程序,看看这是否是你想要的。全屏显示有此代码

primaryStage.setFullScreen(true);
在css文件中,执行以下操作

.root { -fx-background-color:rgba(0,0,0,1); }
您可以在
rgba(0,0,0,1)

中更改数字来更改颜色,这对我很有用

Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.show();
你只需要两样东西:


相关内容:。在JavaFX10上,它不适用于我,
场景。setFill
需要
绘制
而不是
int
@GuillaumeF。
Color
派生自(它是一个子类)。是一种
颜色
(它是一种
油漆
),而不是
int
。因此,将填充设置为透明颜色是可以的。除非场景中涉及
控件,否则此解决方案应该可以正常工作,在这种情况下,还需要其他答案中讨论的附加CSS设置。JavaFX教程在提到此类内容时太不恰当了。感觉他们的代码失败50%的时间是因为这样的“惊喜”:(花了2个小时,直到我放弃,并找到了你的解决方案。谢谢伙计!
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.show();
scene.setFill(Color.TRANSPARENT);
stage.initStyle(StageStyle.TRANSPARENT);