简体   繁体   中英

Why does setVisible() remove my components?

So in the following code, putting frame.add(OpeningSplash) before SetVisible(true) works properly (that is, displays the image) but putting it after doesn't. How would I fix this?

public static void main (String Args[]){
    JFrame frame = new JFrame("Swords & Sworcery");               
    frame.setSize(1920,1080);
    frame.setUndecorated(true);
    frame.setVisible(true);
    frame.add(new OpeningSplash());

    final JFXPanel fxPanel = new JFXPanel(); 
    frame.add(fxPanel); 
    initFX(fxPanel); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    MUSIK.playIntro();

}

It is quite simple to understand. Think of all of your components as peripherals to your computer. You are essentially putting your computer ON before attaching all of the peripherals.

setVisible() completes the addition of all entities to your JFrame so that when you display, everything is intact.

Refer to "Why to use setVisible() below our code!"

So you should invoke your adding of components before setting the frame visible.

Hope this clears up things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM