Java Swing drawing gif as background to several JButtons and JLabels

I want to draw a gif as background to the starting window of my java project (small game in Java swing for school). Here’s my code so far:

package graphic;

import javax.swing.*;
import java.awt.*;
import java.io.File;

//Frame di inizio gioco usato per la selezione del numero di giocatori e la modalità di gioco
public class selectionWindow extends JFrame {

    //Panelli
    private final JPanel logoPanel = new JPanel();
    private final JPanel selectionPanel = new JPanel();
    private final JPanel offlinePanel = new JPanel();
    private final JPanel onlinePanel = new JPanel();
    private final JPanel foregroundPanel = new JPanel();
    private final JPanel backgroundPanel = new JPanel();
    //Componenti offline
    private final JButton offlineButton = new JButton("Offline");
    private final JButton offlineButton_twoPlayer = new JButton("2 Players");
    private final JButton offlineButton_threePlayer = new JButton("3 Players");
    private final JButton offlineButton_fourPlayer = new JButton("4 Players");
    private final Component[] offlineComponents;
    //Componenti online
    private final JButton onlineButton = new JButton("Online");
    private final JButton onlineButton_twoPlayer = new JButton("2 Players");
    private final JButton onlineButton_threePlayer = new JButton("3 Players");
    private final JButton onlineButton_fourPlayer = new JButton("4 Players");
    private final JButton joinButton = new JButton("Join Game");
    private final Component[] onlineComponents;;
    //Immagine decoration
    ImageIcon imageIcon = new ImageIcon( "graphics"+File.separator+"decoration"+File.separator+"BombermanLogo.png");
    JLabel logoLabel = new JLabel(imageIcon);
    //Immagine di sfondo
    ImageIcon backgroundGIF = new ImageIcon("graphics"+File.separator+"decoration"+File.separator+"background.gif");
    JLabel backgroundLabel = new JLabel(backgroundGIF);
    //Font
    Font pixelFont;

    public selectionWindow(){  // ForegroundPanel contiene logoPanel e selectionPanel ed ha un layout a griglia 2x1
        //Caratteristiche e comportamento della frame
        super("BomberMan Game");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setSize(800, 600);
        //this.setLayout(new FlowLayout());
        // Posiziona la finestra al centro dello schermo
        this.setLocationRelativeTo(null);

        //Background Panel
        backgroundPanel.add(backgroundLabel);
        this.add(backgroundPanel);
        //this.setComponentZOrder(backgroundPanel, 1);

        //Foreground Panel
        foregroundPanel.setLayout(new GridLayout(2,1));
        foregroundPanel.add(logoPanel);
        foregroundPanel.add(selectionPanel);
        this.add(foregroundPanel);
        //this.setComponentZOrder(backgroundPanel, 0);

        //JPanel per la decorazione
        logoPanel.add(logoLabel);

        //JPanel per la selezione
        selectionPanel.setLayout(new GridLayout(1,2));
        selectionPanel.add(offlinePanel);
        selectionPanel.add(onlinePanel);

        //JPanel per la selezione offline
        offlinePanel.setLayout(new BoxLayout(offlinePanel, BoxLayout.Y_AXIS));
        offlinePanel.add(offlineButton);
        offlinePanel.add(offlineButton_twoPlayer);
        offlinePanel.add(offlineButton_threePlayer);
        offlinePanel.add(offlineButton_fourPlayer);
        //Centrare gli elementi nel pannello (perchè il box layout altrimenti li allinea a sinistra)
        offlineButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineButton_twoPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineButton_threePlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineButton_fourPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        offlineComponents = offlinePanel.getComponents(); //Lista dei componenti del panello offline (viene poi utilizzata per i listener)

        //JPanel per la selezione online
        onlinePanel.setLayout(new BoxLayout(onlinePanel, BoxLayout.Y_AXIS));
        onlinePanel.add(onlineButton);
        onlinePanel.add(onlineButton_twoPlayer);
        onlinePanel.add(onlineButton_threePlayer);
        onlinePanel.add(onlineButton_fourPlayer);
        onlinePanel.add(joinButton);
        //Centrare gli elementi nel pannello (perchè il box layout altrimenti li allinea a sinistra)
        onlineButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineButton_twoPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineButton_threePlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineButton_fourPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
        joinButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        onlineComponents = onlinePanel.getComponents(); //Lista dei componenti del panello online (viene poi utilizzata per i listener)

        //Carica il font
        pixelFont = new pixelFont().getPixelFont();

        //Gestione componenti offline
        offlineActionListener offlineAL = new offlineActionListener(offlineComponents,onlineComponents, this); ;
        for (Component component : offlineComponents){
            JButton button = (JButton) component;
            if (button != offlineButton) { //Escludo il bottone offline perchè deve rimanere sempre visibile e attivo
                button.setVisible(false);
                button.setEnabled(false);
            }
            button.setFont(pixelFont);
            button.setContentAreaFilled(false); //Rimuove il background del bottone
            button.setBorderPainted(false); //Rimuove il bordo del bottone
            button.addActionListener(offlineAL);
        }

        //Gestione componenti online
        onlineActionListener onlineAL = new onlineActionListener(onlineComponents,offlineComponents, this); ;
        for (Component component : onlineComponents){
            JButton button = (JButton) component;
            if (button != onlineButton) { //Escludo il bottone online perchè deve rimanere sempre visibile e attivo
                button.setVisible(false);
                button.setEnabled(false);
            }
            button.setFont(pixelFont);
            button.setContentAreaFilled(false); //Rimuove il background del bottone
            button.setBorderPainted(false); //Rimuove il bordo del bottone
            button.addActionListener(onlineAL);
        }
        //Modifica icona della finestra
        this.setIconImage(new ImageIcon("graphics"+File.separator+"decoration"+File.separator+"icon.png").getImage());
    }

    public static void main(String[] args) {
        selectionWindow window = new selectionWindow();
    }
}



I tried to use a JLayeredPane and the method setComponentZOrder but i can’t manage to make them work, what am i missing?
I’m 100% sure all the addresses are right (if i comment this.add(foregroundPanel) the gif is correctly shown).
Thanks for any responses <3

I tried to use a JLayeredPane and the method setComponenetZOrder (in those cases i would also add foregroundPanel.setOpaque(false)), expecting them organize the window in such a way so all the contents of foregroundPanel were visible and the gif would play in the background without overlapping any other components.
I tried to implement them in many different ways, sometimes completely changing the structure of the GUI but i always got the same two results: only one of the panels was visible or the backgroundPanel would be above the foregroundPanel and i only could see the buttons for a split second when i hovered my mouse over them.

New contributor

shōgun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật