-1

I write this article because my prior question is closed with wrong answer link.

I couldn't see image on JLabel below code

import javax.swing.*;

public class JButtonEx extends JFrame{

JButtonEx() {
    setSize(300,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel(new ImageIcon("network/star.png"));
    add(label);
    setVisible(true);
}

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

1 Answers1

-1

when u load image, use getClass().getResource();

ImageIcon icon = new ImageIcon(getClass().getResource("star.png"));
  • The accepted answer on the duplicate marked in your previous question has the code line `ImageIO.read(getClass().getResource("/wood.jpeg"));`. Not only does it address the main problem with your code (using `getResource()`), it also features the use of `ImageIO` to load the image, which this answer does not. When code attempts to load an image by `ImageIO` and fails, a helpful stack trace results. As such, this answer provides *less* value to later viewers than the duplicate. It is noise. – Andrew Thompson Oct 31 '21 at 15:56
  • but when i use ImageIo, it doesn't work – Byeongsu KIM Nov 01 '21 at 14:55