-2
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JPanel;

public class FirstFrame extends JFrame {

    // FirstFrame properties

    public FirstFrame() {

        setTitle("Stacker");
        setLayout(new GridBagLayout());
        setSize(380, 650);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUndecorated(true);
        setVisible(true);
        setResizable(true);

        JLabel background = new JLabel(new ImageIcon("fw.jpg"));
        background.setLayout(new BorderLayout());
        add(background);

    }

    public static void main(String[] args) {
        new FirstFrame();
    }
}

Okay now that I have the frame undecorated .. (thanks to you guys) I'm trying to put a background image for the frame. but when I run it, all it shows is a blank frame.
What is wrong? How to correct it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
dan
  • 27
  • 1
  • 2
  • Its probably because the ImageIcon can't find the "fw.jpg" in the path. Where is this "fw.jpg" file located? – Aman Agnihotri Feb 23 '14 at 07:53
  • By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Feb 23 '14 at 08:34

2 Answers2

2

Please consider doing the following..
1. Add JLabel, background before you setVisible(true).In fact, put the setVisible as the last line in your constructor.
2. Then try to get the image as
getClass().getResource(imgPath)
set the image

1

You're likely not looking for the image in the right location. If you're going to look for the image as a file (which you're currently trying to do), you need to do so relative to the user's directory which can be found via System.out.println(System.getProperty("user.dir")). If you're going to look for it as a resource, you will need to look for it relative to the class file locations. This has all be discussed and rehashed ad-infinitum on this site. Please search some more.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373