Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a program that first starts up a little splash screen, and after 5 seconds it continues to a new Stage.

The strange thing is, until now it worked fine, but now that I'm starting to build the new Stage I'm declaring new Nodes in the class to use it.

Now all of the sudden the splash screen has a white background, commenting the before created nodes out will revert the image back to being transparent.

public static Pane pane = new Pane(); //this is fine

//Uncommenting this Node will change the splash screen background white
//public static TextArea textArea = new TextArea();

public static Image         splashImage     = new Image(Class.class.getClassLoader().getResourceAsStream("image.png"));
public static ImageView     splashImageView = new ImageView(splashImage);
public static BorderPane    splashPane      = new BorderPane(splashImageView);
public static Scene         splashScene     = new Scene(splashPane);

@Override
public void start(Stage primaryStage) throws Exception {

    splashScene.setFill(Color.TRANSPARENT);

    primaryStage.setScene(splashScene);
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.show();

}

Here a shortened version of my code

Could this be because of memory issues or something? I have never encountered anything like this.

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
480 views
Welcome To Ask or Share your Answers For Others

1 Answer

The default style sheet is only loaded if a control (i.e an instance of Control or one of its subclasses) is created. (The idea behind this is to avoid the performance overhead of loading CSS for applications that manage all their own graphics and don't use any controls, such as games or simulations.)

The default stylesheet sets the background color of the root node (the splashPane in your example) to a very light grey (specifically, it is 26.4% brighter than the color #ececec).

Since the text area is the only control in your class, creating it causes the default style sheet to be loaded, which sets the background color of the splashPane to a very light grey.

If you need to instantiate controls and want the background of the pane to be transparent, you need to specify that in an external CSS:

splashStyle.css:

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

And

@Override
public void start(Stage primaryStage) throws Exception {

    splashScene.setFill(Color.TRANSPARENT);
    splashScene.getStylesheets().add("splashStyle.css");

    primaryStage.setScene(splashScene);
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.show();

}

(As a quick check that this will work, you can just test with

splashPane.setStyle("-fx-background-color: transparent; ");

)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...