I have problems with adding images to JFrame java application. Now I am using Eclipse IDE.

Below is the code that no images is shown.

import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class LabelDemo {
    public static void main(String[] args) {
        JLabel northLabel = new JLabel("North");
        ImageIcon labelIcon = new ImageIcon(\\resources\\CS2103_Logo.png");
        JLabel centerLabel = new JLabel(labelIcon);
        JLabel southLabel = new JLabel(labelIcon);

        southLabel.setText("South");

        JFrame application = new JFrame();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        application.add( northLabel, BorderLayout.NORTH);
        application.add(centerLabel, BorderLayout.CENTER);
        application.add(southLabel, BorderLayout.SOUTH);

        application.setSize(300,300);
        application.setVisible(true);

    }
}

After typing code, I right click on src folder to add subfolder called "resources" and add new file and link to my CS2103_LOGO.png

However, if I change the code from

        ImageIcon labelIcon = new ImageIcon(\\resources\\CS2103_Logo.png");

to

   ImageIcon labelIcon = new ImageIcon("D:\\JAVAworkspace\\CH6HowtoProgram\\src\\resources\\CS2103_Logo.png");

It will work well but I do not want. Because, I would like this application to be able to be used in different locations as well.

I also have one addition problem about GUI design.
If my design have one main body but after click a button it will expand another panel showing different information. Should I use big JFrame and then add panel later? Or should I seperate into many JFrame.

Thank you very much for your help :)

First; for you to take the image from the relative path your folder should be in the path D:\JAVAworkspace\CH6HowtoProgram\resources

or to take the relative path it should be like

\src\resources\CS2103_Logo.png

I also have one addition problem about GUI design.
If my design have one main body but after click a button it will expand another panel showing different information. Should I use big JFrame and then add panel later? Or should I seperate into many JFrame.

Use one JFRAM ; unnless it is neceaasry do not use multiple JFrames

Thank you for your answer... But the image still cannot be shown...

I would like to take the relative path so what should i try next?

Try using the path in a File object and print out the FIle object's absolute path to see where the app is looking for the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.