Setting Loaded GIF as a Background

前端 未结 3 920
时光取名叫无心
时光取名叫无心 2021-01-22 06:50

Situation: I have a JFrame and I have managed to import the GIF and display it in the Main Panel however it moves all of my other panels down, causing my GUI to be

3条回答
  •  心在旅途
    2021-01-22 07:31

    UPDATED ANSWER

    Now in accordance with Oracle's Swing UI-guidlines as correctly indicated by @MadProgrammer

    Best way to add a background is to simply paint it. Create a BackgroundPanel (extends JPanel) and override paintComponent(...);

    public class BgPanel extends JPanel
    { 
        //Overload and paint your background
        public void paintComponent(Graphics g)
        {
            //create image object, or more preferably do that in the BgPanel constructor
            g.drawImage(image, 0, 0, null);
        }
    }
    

    In accordance with Oracle's guidelines for custom rendering when using Swing.

提交回复
热议问题