import java.awt.BorderLayout; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JPanel; public class GUIFrame extends JFrame{ JLayeredPane layerPane = new JLayeredPane(); BackgroundPanel gameBackground = new BackgroundPanel(loadImage("Background.png"), this); JButton testButton = new JButton("TEST BUTTON"); JPanel testPanel = new JPanel(); public GUIFrame(String frameTitle) { super(frameTitle); //Set BorderLayout setLayout(new BorderLayout()); //Put testButton inside testPanel testButton.setVisible(true); testButton.setSize(500, 50); testPanel.add(testButton); testPanel.setSize(500, 50); //Add gameBackground to default layer of layerPane layerPane.add(gameBackground, JLayeredPane.DEFAULT_LAYER); //Add testPanel to the layer above default layerPane.add(testPanel, JLayeredPane.PALETTE_LAYER); //Add layerPane to JFrame add(layerPane); /* If I uncomment these to lines and comment the line * it will work but then the button will be in the same layer */ //add(testPanel); //add(gameBackground); } //Simple image loading method; Returns buffered image public static BufferedImage loadImage(String fileLocation){ BufferedImage image = null; try{ image = ImageIO.read(new File(fileLocation)); }catch(IOException exceptionReturned){ System.out.println("Image loading exception when loading " + fileLocation); } return image; } }