0

When I set the background image, my buttons and everything get hidden. The only thing I see is the image I chose.

This is the code I used:

this.setContentPane(new JLabel(new ImageIcon("image.jpg")));

Also, when I design a background, do I have to design it exactly on the size of my program?

Joel
  • 4,732
  • 9
  • 39
  • 54
user3102872
  • 529
  • 2
  • 5
  • 9
  • Your image gets drawn over your buttons, read [this](http://stackoverflow.com/questions/5636170/jbutton-is-drawing-behind-an-image) – Ceiling Gecko Dec 17 '13 at 16:50
  • 1
    `setContentPane()` replaces the content. Your code is doing exactly what you told it to. – SLaks Dec 17 '13 at 16:51

2 Answers2

2
this.setContentPane(new JLabel(new ImageIcon("image.jpg")));

You can use a label as a background and then add components to the label. Your code should be something like this:

JLabel background = new JLabel(new ImageIcon("image.jpg"));
background.setLayout( new BorderLayout() );
background.add(...);
setContentPane( background );
camickr
  • 321,443
  • 19
  • 166
  • 288
0

If you use it the way you do the Contentpane isn't a container anymore!

For this you need to implement your own subclass of JComponent which overrides the paintComponent() method!

PrR3
  • 1,250
  • 1
  • 18
  • 26