简体   繁体   中英

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:

空白窗口

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

在此处输入图片说明

what am I doing wrong?

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

You have set the visibility of JFrame at a very early stage. At that time the JLabel was not added. When you minimized and resized your frame, it got rendered again resulting in showing your added components. Remember to add components before setting the Frame's visibility( set visibility at last).

Also I would suggest you to use GUI threads when working on swing components. Refer to swing utilities here : https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

Lastly set the layout of JFrame to null as you are trying to add labels to specific coordinates with setBounds() method.

Default layout of jframe is BorderLayout , so if you want you can change layout by reference of java.awt.Container abstract class.

It is initialized by getContentPane() of javax.swing.JFrame class. The components are added only through reference of java.awt.Container class.

java.awt.Container c=cpec.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
c.add(l); //label will get added to JFrame instance that is referenced
//then define size and at last define visibility
cpec.setSize(500, 500);
cpec.setVisible(true);

Set the Layout manager of the container as null. By default it uses BorderLayout as its Layout manager. You just have to call the getContentPane() method using the reference of the JFrame, which returns a container reference. Example:

Container c = frame.getContentPane();
c.setLayout(null);

For more information you can go through my Website .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM