Reputation: 1
I am having an issue with getting my image to display within the GUI window. The GUI initiates fine but no image is displayed window displays no image. I have tired various methods with no luck and I'm not sure where I'm going wrong. Any guidance would be greatly appreciated
package banksystem;
import javax.swing.*;
import java.awt.*;
public class Login extends JFrame {
Login(){
setTitle("Cash Machine");
ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("bank-icon.jpg"));
Image img2 = img1.getImage().getScaledInstance(100,100, Image.SCALE_DEFAULT);
ImageIcon img3 = new ImageIcon(img2);
JLabel label = new JLabel(img3);
add(label);
setSize(800, 480); //Sets frame size
setVisible(true); //Frame will display
setLocation(350, 200); //Sets frame location
}
public static void main(String[] args) {
new Login();
}
}
Upvotes: 0
Views: 375
Reputation: 402065
ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("/images/bank-icon.jpg"));
Also, move resources directory outside of the sources root.
Upvotes: 2