View Single Post
  #1 (permalink)  
Old 03-07-06, 12:35 AM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Background Images and JLayeredPane

I would like to create a background image in my GUI using a JLayeredPane. I have the code shown below and it doesnt display anything at all. Could somebody please help me out.

Code:
import java.awt.*;
import javax.swing.*;
 
public class ImagesExample {
    
		public ImagesExample() {
		
			JFrame frame = new JFrame();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			JLayeredPane layers = new JLayeredPane();
			JPanel topPanel = new JPanel();
			
			JLabel bgIm = new JLabel(new ImageIcon("imagepath.jpg"));
			
			layers.add(bgIm, new Integer(1));
			layers.setOpaque(true);
			
			topPanel.add(layers);
			topPanel.setOpaque(true);
			
			JComponent newContentPane = topPanel;
	                newContentPane.setOpaque(true); 
	                frame.setContentPane(newContentPane);
	
	                //Display the window.
	                frame.pack();
	                frame.setVisible(true);			
		}
		
   public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				new ImagesExample();
			}
		});
    }
}
Reply With Quote