问题
How do i programmatically make a Node's background transparent-(see through); I am currently playing with AnchorPane as a parent Node to other ProgressIndicator and other nodes as children, and i want them to stand out ? I tried with Scene and it wasn't close to what i wanted, any workaround?
lets say
// suppose i have a fake layout like this
AnchorPane ap = new AnchorPane();
ProgressIndicator pi = new ProgressIndicator();
ProgressIndicator pii = new ProgressIndicator();
ProgressIndicator piii = new ProgressIndicator();
ProgressIndicator piiii = new ProgressIndicator();
AnchorPane.setRightAnchor(pi, 0.0);
AnchorPane.setBottomAnchor(piii, 0.0);
AnchorPane.setRightAnchor(piiii, 0.0);
AnchorPane.setBottomAnchor(piiii, 0.0);
AnchorPane.setLeftAnchor(piii, 0.0);
Circle circle = new Circle();
circle.setRadius(50);
circle.setFill(Color.RED);
AnchorPane.setBottomAnchor(circle, 210.0);
AnchorPane.setTopAnchor(circle, 210.0);
AnchorPane.setLeftAnchor(circle, 210.0);
AnchorPane.setRightAnchor(circle, 210.0);
ap.getChildren().addAll(pi,pii,piii,circle,piiii);
primaryStage.setScene(new Scene(ap,500,500));
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
My requirement here is to make my children Nodes stand out,without the white background of the AnchorPane -(so AnchorPane needs to be transparent), how do i achieve that?
回答1:
Everything including Stage, Scene and Layout Containers have their background color. So if you need a complete transparent background, you need to set transparent fill to each of these.
For Stage
primaryStage.initStyle(StageStyle.TRANSPARENT);
For Scene
scene.setFill(Color.TRANSPARENT);
For Containers
container.setBackground(Background.EMPTY);
For your code
Scene scene = new Scene(ap,500,500);
scene.setFill(Color.TRANSPARENT);
ap.setBackground(Background.EMPTY);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
回答2:
You should first understand how the JavaFX view hierarchy works. First, you have a Stage. In a stage, you can display a Scene. In a scene, you can embed a Node, which will typically be a pane or sorts, like a BorderPane. In that case, you should make the pane transparent, as it is the content of the scene of the stage.
Use setStyle("-fx-background-color: transparent;");, or better yet: add that to a class in a css file, set that css file as the style sheet of the scene containing your Node and add the css class to the styleClasses of the Node.
来源:https://stackoverflow.com/questions/29004893/transparent-node-background