Add image to JLabel

Answered

Hey Intellij community,

I hope you don't mind me asking a question that might have been asked before, I'm on a journey to learn to write JAVA applications and I ran into the following issue.

I can create a JFrame and add a JLabel but when I want to add a random picture it just doesn't show up, I added text to the label as well and it does seem to move when the image is called but it is just not visible.

Below you'll find the code I've added following this online tutorial, if you could point out what I'm doing wrong that would be great.

import javax.swing.*;


public class Main {

public static void main(String[] args) {

ImageIcon image = new ImageIcon("manSad.jpg");

JLabel label = new JLabel();
label.setText("Bro, do you even code?");
label.setIcon(image);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);


JFrame frame = new JFrame();
frame.setTitle("This is a noob app");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.add(label);


}
}
Source : https://www.youtube.com/watch?v=FR2UptJyaSM

0
1 comment

Hi Edwinlangendam This forum is for IntelliJ IDEA related questions only. Please use https://stackoverflow.com for discussing Java-related ones. Thank you.

0

Please sign in to leave a comment.