How do I add a background image to JPanel using Swing?? HELP!?

shabooby12

New member
Okay...before I get a swarm of comments saying "You cannot add an image to a JPanel, you must make a JLabel and set it as an image and add the JLabel to the Container...", etc...

I'm stuck and frustrated as to why this isn't appearing. Here's my source code so far...




/******************************************
*Auteur:Christophe
*Fichier: Cachotterie.java
*****************************************/


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

public class Cachotterie extends JPanel
{
static JFrame CachotterieFrame;
static Container CachotterieContainer;
static JLabel jLabel;
static JPanel jPanel;

public static void main (String[] args)
{
Cachotterie cach = new Cachotterie();
cach.setupGUI();
}

public void setupGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
CachotterieFrame = new JFrame("Cachotterie!");
CachotterieFrame.setSize(556, 378);
CachotterieFrame.setLocationRelativeTo(null);
CachotterieFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CachotterieFrame.setVisible(true);

ImageIcon img = new ImageIcon("bgImg.jpeg");
jLabel = new JLabel();
jLabel.setIcon(img);
jLabel.setHorizontalAlignment(JLabel.CENTER);
jLabel.setOpaque(true);

jPanel = new JPanel();
jPanel.setBounds(10, 10, 400, 200);
jPanel.setLayout(null);

CachotterieContainer = CachotterieFrame.getContentPane();
CachotterieContainer.setLayout(null);
CachotterieContainer.add(jPanel);
CachotterieContainer.add(jLabel);
CachotterieContainer.setVisible(true);

}
}
 
Back
Top