how to make transparent scene and stage in javafx?

戏子无情 提交于 2019-11-27 08:46:01

For modena.css (the default JavaFX look and feel definition in Java 8), a slight shaded background was introduced for all controls (and also to panes if a control is loaded).

You can remove this by specifying that the default background is transparent. This can be done by adding the following line to your application's CSS file:

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

This is in addition to other settings you already have in your code to initialize the style of the stage and background fill of the scene.

stage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(Color.TRANSPARENT);

Note: in the questions's sample code, an additional stage (initStage) is created instead of using the passed in stage for the start method. The passed in stage can be initialized, utilized and shown directly by your code rather than creating an additional initStage.

stage.initStyle(StageStyle.TRANSPARENT);
this is for hide the top bar ( minimize, Restore Down and close)

scene.setFill(Color.TRANSPARENT);
this is for the frame color ( you can replace TRANSPARENT with any color GREEN YELLOW RED BLUE ...)
but for me I want glass view if you can understand me,
and with different color
so the solution is

 primaryStage.setOpacity(0.2);


the number 0.2 is between 0 and 1
0 is hidden and 1 is normal form but between the numbers transparent
so choose your number and run your program and see if this is what you want
there is this code for full screen

 primaryStage.setFullScreen(true);<br>

and in the css file do this

.root { -fx-background-color:rgba(0,0,0,1); }<br>

and you can change the color with changed the number in rgba(0,0,0,1)

This works for me.

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();

U just need mainly 2 things:

scene.setFill(Color.TRANSPARENT);
stage.initStyle(StageStyle.TRANSPARENT);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!