3

I am using eclipse and I want to add a image onto a label or a logo for my GUI.

I want it to LOOK LIKE THIS!

http://imageshack.us/a/img593/4957/skeletancalcimage.png

I uploaded it onto the web because I dont have 10 reputation.

(Names cool program) is the image

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

    public class MainClass extends JFrame{ // super class JFrame

        // Kind of a tutorial for creating GUI's in java
        private JLabel label;
        private JLabel label1;
        private JButton button;
        private JTextField textfeild;
        private ImageIcon image1; // image for my logo

        public MainClass () {
            setLayout (new FlowLayout());

            image1 = new ImageIcon (getClass ().getResource ("logo.gif")); // declares image

            label = new JLabel ("This is the label");
            add (label);

            label1 = new JLabel (image1); // adds image
            add (label1);

            textfeild = new JTextField (15);
            add (textfeild);

            button = new JButton ("Click");
            add (button);

        }

        public static void main(String[] args) {        

            MainClass gui = new MainClass ();


            gui.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // creates window
            gui.setSize (300,300);
            gui.setVisible (true);
            gui.pack ();
            gui.setTitle ("Title");

        }

    }

Program compiles but doesn't run. Gives me

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at JonsCalc.<init>(JonsCalc.java:18)
    at JonsCalc.main(JonsCalc.java:38)
Fryon Alexandra
  • 139
  • 7
  • 19
  • 1
    `"I am using eclipse and I want to add a image onto a label or a logo for my GUI. I want it to LOOK LIKE THIS!"` ... OK, so what is your specific question? What happens when you try? An exception? It doesn't compile? Your computer blows up into a 1000 bits? – Hovercraft Full Of Eels Dec 23 '12 at 21:45
  • So is that the image you want to put into your program, or does "NAMES COOL PROGRAM" represent the image you want to add? – Nick ODell Dec 23 '12 at 21:45
  • 2
    I'm guessing at your problem (and we shouldn't have to be doing this!), but perhaps your current GUI doesn't lay out the way you desire. If so, then you'll want to go to the [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) to learn how to use the various layouts that are available for Swing GUI's. After you're done with this, consider downloading the MigLayout. – Hovercraft Full Of Eels Dec 23 '12 at 21:50
  • If the problem is that the image does not appear check that you have `logo.gif` in the same place as the `MainClass`. – Adrian Ber Dec 23 '12 at 21:57
  • 1
    The problem with `ImageIcon` is it does not report, in any meaningful manner, when it can't load the image. Try using `getClass ().getResource ("/logo.gif")` instead and use the `ImageIO` API, as it will at least throw an `Exception` if it can't load the image – MadProgrammer Dec 23 '12 at 22:00
  • @FryonAlexandra : Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), this might be of some interest to you :-) – nIcE cOw Dec 24 '12 at 14:15

4 Answers4

1

This piece of code getClass().getResource ("logo.gif") means that the image should be loaded from the same location as your class MainClass has. Check that you image is in the same package as MainClass. If you use Eclipse, than put image file in the package in src folder, it will be copied to bin automatically.

tcb
  • 2,745
  • 21
  • 20
1

I suggest you create a package exclusively for your images/icons, then create a single loader class there named 'ImageLoader' or whatever you want. You can then use this code...

new ImageLoader().getClass().getResource("NAME_OF_IMAGE"));
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Mark Vizcarra
  • 1,165
  • 1
  • 11
  • 15
0

Right click on your class then go to new. Click on new Source Folder. Finally move your images in that folder. Now you can use that code.

Luke Worthing
  • 101
  • 1
  • 4
  • 12
0

If you are using Eclipse, copy your images to your package and your project's bin folder. Then just type image1 = new ImageIcon(this.getClass().getResource("filename.filetype"));

NoName
  • 1