0

Here is what I have so far:

ImageIcon image = new ImageIcon("Img.png");
JLabel label = new JLabel(image);
label.setText("test");
label.setIcon(image);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.TOP);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(label);
frame.setVisible(true);

Just a super simple setup. I've imported Img.png but nothing seems to work.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Have you checked that the image exists in the location you think it does? This is a common issue, so the first step is to see if you code can see the image, try `File test = new File("Img.png");` then `System.out.println("The file exists: "+ test.exists());`, now you know that the image can't be found, so you need to look into where you actually put the image, and where you need to put it. Likely it should be imported as a resource, and loaded using `getClass().getResourceAsStream("res/Img.png");` – sorifiend Jun 18 '21 at 01:04
  • This answer is very helpful on loading images: https://stackoverflow.com/questions/9864267/loading-resources-like-images-while-running-project-distributed-as-jar-archive/9866659#9866659 – sorifiend Jun 18 '21 at 01:16
  • `label.setIcon(image.getImage())` should work. –  Jun 18 '21 at 09:06

0 Answers0