I am trying to add image in JLabel but it’s not working. The second label is working but the first JLabel is not working.
Here is code.
Thanks in advance.
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainLabel {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jframe;
jframe = createFrame();
ImageIcon ii = new ImageIcon("images.jpeg");
JLabel label = new JLabel(ii);
jframe.add(label);
Label label123 = new Label("Be Nice to World!!");
jframe.add(label123);
jframe.setVisible(true);
}
static JFrame createFrame() {
JFrame guiFrame = new JFrame();
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("BorderLayout Example");
guiFrame.setSize(700, 300);
return guiFrame;
}
}
Your
labelwhich contains the image is being replaced withlabel123in theBorderLayout.CENTERposition, which doesnt have any image attached. You could use:If you want the 2 labels to be shown, you could place the text-based
label123in theSOUTHlocation:Note: Use
JLabelinstead ofLabel.