Background for a frame

Ranjith
Ranjith used Ask the Experts™
on
I would like to add an image as a background of a frame but still be able to add things on top of it. I mean the image frame should act like a normal frame with the background image, but still i should be able to add label or whaterver like a normal frame. Thank You
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
import java.awt.event.*;import javax.swing.*;import java.awt.*;public class TestBackground extends JFrame{     ImageIcon img;     public TestBackground()     {          img = new ImageIcon("????.jpg");          JPanel panel = new JPanel()          {               public void paintComponent(Graphics g)               {                    g.drawImage(img.getImage(), 0, 0, null);                    super.paintComponent(g);               }          };          panel.setOpaque(false);          panel.add( new JButton( "Hello" ) );          setContentPane( panel );     }     public static void main(String [] args)     {          TestBackground frame = new TestBackground();          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          frame.setSize(300, 300);          frame.setVisible(true);     }}
What a mess!
No offense.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Top Expert 2016

Commented:
Ugh, sorry about that formatting! This is better:

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

public class TestBackground extends JFrame{
  ImageIcon img;

  public TestBackground(){
    img = new ImageIcon("ex-eng.gif");
    JPanel panel = new JPanel(){
      public void paintComponent(Graphics g){
        g.drawImage(img.getImage(), 0, 0, null);
        super.paintComponent(g);
      }
    };
    panel.setOpaque(false);
    panel.add( new JButton( "Hello" ) );
    setContentPane( panel );
  }

  public static void main(String [] args) {
    TestBackground frame = new TestBackground();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}

Author

Commented:
Thanks a lot man. It works!!

Author

Commented:
Thanks a lot man. It works!!
Top Expert 2016
Commented:
That's OK.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial