Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to set an image as background using Java on Netbeans

807589Nov 14 2008 — edited Nov 16 2008
Hi Expert,
I am a newbies in java and i am trying to creat an image as a background on a GUI i created, but i am not getting any image dispalying as bacground.
Here is my code:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class NewClass1 extends javax.swing.JFrame {
    public NewClass1() {
        initComponents();
            ImageIcon image = new ImageIcon("C:\\myimage.jpg");
		JLabel background = new JLabel(image);
		background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
		getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
		JPanel panel = new JPanel();
                panel.add(background);
                //panel.add(getLayeredPane);
		panel.setOpaque(false);
		setContentPane( panel );
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewClass1 JFrame = new NewClass1();
                JFrame.setVisible(true);
                JFrame.setBounds(20, 20, 600, 400);
                JFrame.setDefaultCloseOperation(NewClass1.EXIT_ON_CLOSE);
		JFrame.setSize(200, 689);
            }
        });
    }

    private void initComponents() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
Can some check to see why i am not getting an image. If possible modify my code to give me the correct one that can get the image.
Thanks

Edited by: m_l on Nov 14, 2008 7:29 AM

Edited by: m_l on Nov 14, 2008 7:30 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 14 2008
Added on Nov 14 2008
24 comments
5,225 views