Home > Blockchain >  Background only showing after minimized and opened back window in java swing
Background only showing after minimized and opened back window in java swing

Time:11-06

My code works but the background only shows after I minimize and click back on the java app in the taskbar. I am using Visual Studio


import javax.swing.\*;
import javax.swing.plaf.DesktopPaneUI;

import java.awt.\*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class App {
private static ImageIcon shitdowsWallpaper;
public void initialize() {
JFrame desktopFrame = new JFrame();
JPanel desktopPanel = new JPanel();

        desktopFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        desktopFrame.setTitle("shitdows");
        desktopFrame.setSize(1280, 720);
        desktopFrame.setResizable(false);
        desktopFrame.setVisible(true);
        JLabel background=new JLabel(new ImageIcon("src\\windows_default_wallpaper.png"));
        desktopFrame.add(background);
    }
        
    public static void main(final String[] args) {
        App myFrame = new App();
        myFrame.initialize();
    }

}

I want the background to show instantly without doing anything extra.

CodePudding user response:

Just by adding the panel it isn't automatically rendered, you have to tell swing to actually render it. You can do that by either manually calling frame.repaint() or you can add the panel before calling frame.setVisible(true) since this is already rendering the components on the frame.

  • Related