Results 1 to 3 of 3
  1. #1
    newtojava7 is offline Member
    Join Date
    Jan 2008
    Posts
    16
    Rep Power
    0

    Default GUI... setting my background to an image, im using a JFrame

    how do I set the background as an image? i already have the image i want in the same folder.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    This may help you. I used ImageIcon here, but not tested. Just writing this code on my memory :)

    Java Code:
    ImageIcon icon = new ImageIcon(your_image_path_here);
    backgroundImage = icon.getImage();
    After that get a content pane for your GUI(components actually.) There is a method called setBackgroundImage() where you can set the backgroundImage there.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Using a Jpanel you can easily done this. Why I suggest to use a JPanel is, on top of that you can easily work on with other controls. In some cases of my projects I used JLables also. Add a image on that and do my process. But on a Lable I can't add any other component. But in JPanel you can do it. On working with JLables ToolKit gives a nice support.

    Java Code:
            ImageIcon imageBack = new ImageIcon(your_image_Path_here);
            JLabel background = new JLabel(imageBack);
            background.setBounds(0, 0, imageBack.getIconWidth(), imageBack.getIconHeight());
    	getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
            jPanel1.setOpaque(false);
            setContentPane(jPanel1);
    See how I use a Lable and JPanle used here. I think it is clear to you.

Similar Threads

  1. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  2. Center background image
    By Floetic in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 05:50 PM
  3. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 07:15 AM
  4. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:24 AM
  5. Problem Setting offscreen background
    By D34N0 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2007, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •