0

I'm new to using GUI in java; I'm trying to change the Image Icon for my Project but I don't know why it's not working. I'm using IntelliJ for this, I moved the image(CurrencyExchange.png) for the Icon to the "src" Folder in my Project.

import javax.swing.*;
import java.awt.*;


public class main {

    public static void main(String[] args){

        JFrame Frame1 = new JFrame();

        Frame1.getContentPane().setBackground(new Color(0x123456));
        Frame1.setTitle("Currency Changer");
        Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame1.setSize(500,500);
        Frame1.setResizable(false);
        Frame1.setLocation(430,100);
        Frame1.setVisible(true);

        ImageIcon image = new ImageIcon("CurrencyExchange.png");
        Frame1.setIconImage(image.getImage());

    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
N Ib
  • 1
  • 2
  • 1
    You will probably want to distribute or simply run your application from JAR file, in which case it is better if you learn how to load *resources*. Related: [Loading resources like images while running project distributed as JAR archive](https://stackoverflow.com/q/9864267) (accepted answer there seems to also have info about IntelliJ settings for this) – Pshemo Jan 30 '23 at 19:16
  • thanks, i checked the link you provided and fixed it. – N Ib Jan 30 '23 at 19:26

1 Answers1

0
BufferedImage image = null;
try {
        image = ImageIO.read(Objects.requireNonNull(Main.class.getResource("CurrencyExchange.png")));
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    Frame1.setIconImage(image);
N Ib
  • 1
  • 2