|
Re: Need help in getResource [message #1482009 is a reply to message #1481831] |
Fri, 21 November 2014 11:58 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Neo,
Asking about Swing applications is best done on some general forum such
as Stackoverflow. If you put the image in the same folder as the class,
you can just use "Hello.jpg. I think if you use /Images, it would need
to be in the root of the jar/bin...
On 21/11/2014 10:13 AM, neo sluf wrote:
> Can someone tell me why this is not working?
>
> This is from a tutorial. I read a lot on different threads about this
> problem and I found out that I need to create a new Source Folder. I
> name it to Images then add Hello.jpg to it by dragging the image into
> the folder and it works.
>
> Now they said that in order to use the image you need to put
> "/nameofthefolder/file.filetype/"
> So I put it in the correct path.
>
> But my problem is, it is not working.. My code is very simple.. Add an
> image to an JLabel
>
> I can't put an picture on because I need 10 rep.. but I create a
> Source Folder and add Image to that folder correctly I promise
>
> I know this question sounds stupid but I am very new to programming...
> please have a kind consideration on the beginner..
>
>
> package myGUI102;
> import java.awt.*;
> import javax.swing.*;
> public class myGUIAddImage extends JFrame{
> private ImageIcon image1;
> private JLabel label1;
>
>
> myGUIAddImage (){
> setLayout(new FlowLayout());
>
> image1 = new ImageIcon
> (getClass().getResource("/Images/Hello.jpg"));
> label1 = new JLabel (image1);
> add (label1);
>
>
>
> }
> public static void main(String[] args) {
> myGUIAddImage mygui = new myGUIAddImage();
> mygui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> mygui.setVisible(true);
> mygui.pack();
> mygui.setTitle("My Image");
>
> }
> }
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.07641 seconds