Java GUI not loading images/ImageIcons help

Hey, so im going through the basics of GUI. At first it was going great but right now Ive been trying to a) set the ImageIcon of the frame and also setting the ImageIcon of a label but with my current implementation none of the pictures load and I dont know why. I have included both the code and the file strucutre (ps. i have also set the Pictures package to be Resources Root Directory):
import javax.swing.*;
import java.awt.*;

public class Main {
JFrame frame;
public static void main (String[] args) {
JFrame frame = new JFrame("Wowzzzers");
frame.setSize(420,420);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
ImageIcon img = new ImageIcon("/Pictures/yippee.jpg");
ImageIcon img2 = new ImageIcon("/Pictures/these-days-were-wild-v0-vfwnc6uu1e7e1.webp");
frame.setIconImage(img.getImage());

JLabel label = new JLabel();
label.setText("Wassssupp");
frame.add(label);
label.setForeground(Color.CYAN);
label.setIcon(img2);
label.setVisible(true);

//frame.pack();
}
}
import javax.swing.*;
import java.awt.*;

public class Main {
JFrame frame;
public static void main (String[] args) {
JFrame frame = new JFrame("Wowzzzers");
frame.setSize(420,420);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
ImageIcon img = new ImageIcon("/Pictures/yippee.jpg");
ImageIcon img2 = new ImageIcon("/Pictures/these-days-were-wild-v0-vfwnc6uu1e7e1.webp");
frame.setIconImage(img.getImage());

JLabel label = new JLabel();
label.setText("Wassssupp");
frame.add(label);
label.setForeground(Color.CYAN);
label.setIcon(img2);
label.setVisible(true);

//frame.pack();
}
}
No description
5 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @mjstx! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Patrick Java
Patrick Java2mo ago
place images in resources folder, then load them using getResource() or you can use relative path referencing "/Pictures " uses absolute referencing and tries to locate /Pictures from the root file system
mjstx
mjstxOP2mo ago
alr wait ill give it a try still no luck could you type what you meant
Patrick Java
Patrick Java2mo ago
like ImageIcon img = new ImageIcon(Main.class.getResource("/Pictures/yippee.jpg")); if Pictures is your resources root folder, do like this ImageIcon img = new ImageIcon(Main.class.getResource("yippee.jpg"));
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?