My code is very simple, really there is no problem in writing my code. I'm getting the file not found error as an error, but I'm 100% sure the file path is correct, I've checked hundreds of times. There are no other errors. Some different image upload methods give no error but as a result, the image never appears.
ChatGPT: can't fix.
Image: 16x16, .png, 1.72KB
Compiler IDE: Eclipse
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class main {
public static void main(String[] args) {
JFrame window = new JFrame("JavaTests");
window.setVisible(true);
window.setResizable(true);
window.setSize(800,600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.PINK);
try {
File imageFile = new File("icon.jpg");
BufferedImage image = ImageIO.read(imageFile);
JLabel label = new JLabel(new ImageIcon(image));
label.setSize(image.getWidth(), image.getHeight());
label.setBackground(Color.BLACK);
label.setOpaque(false);
window.add(label);
} catch (Exception e) {
System.out.println("LOG: " + e.getMessage());
e.printStackTrace();
}
}
}