Mcolo Mcolo - 2 months ago 7
Java Question

I am having some issues regarding jframe and jlabel?

I am making simple login screen. I added two JLabel's in JFrame in my program and it's running successfully but the problem is that when I run the program I got blank screen and empty jframe, however I have added two jlabel's in that frame but it's not showing me any thing and then if I minimize the window and after some time if I open that window again then I can see those components.

Here is my code:

package javaapplication41;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.*;

public class JavaApplication41 {


JavaApplication41()
{
JFrame cpec=new JFrame();
cpec.setBounds(300,200,600,350);
cpec.setUndecorated(false);
cpec.setVisible(true);
cpec.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel l = new JLabel(new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\file (2).jpg"));
l.setBounds(100,100,200,125);
//l.setLayout(null);
cpec.add(l);
JLabel kiq=new JLabel(new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\bla.jpg"));
kiq.setBounds(100,100,100,100);
//kiq.setLayout(null);
l.add(kiq);
}
public static void main(String[] args) {
JavaApplication41 ne=new JavaApplication41();
}

}


I am getting this output when I run program:

blank window

and when I minimize this window and again open this, then I am getting the desired output here it is:

enter image description here

what am I doing wrong?

Answer

You have to put cpec.setVisible(true); after adding all the items in your jframe.I hope this will surely solve your problem

Comments