简体   繁体   中英

JPanel inside JFrame doesn't show content sometimes. Why?

I managed to fix it but I don't understand why the same code results in different results. Some classmates have had the same problem.

The issue is that it I use miVentana.setVisible(true); before chicha(); the elements inside the JPanel will show when executing but if I run it again sometimes they won't ve visible until I resize the window, a few times not even the JPanel background color was visible. Just clicking the "Run" bottom on the IDE without changing anything else.

I just tried it 10 consecutive times and the elements were only visible on the 4th attempt.

Could this come from some memory garbage remaining from previous executions of the code?

I'm using Eclipse Version: Photon Release (4.8.0).

This is the code with the weird behaviour:

public class Ej10 extends JFrame {

    public Ej10() {
        setLayout(null);
    }

    static Ej10 miVentana = new Ej10();

    public static void main(String[] args) {
        miVentana.setTitle("Ejercicio10");
        miVentana.setBounds(20, 20, 500, 600);
        miVentana.setLocationRelativeTo(null);

        miVentana.setVisible(true);

        chicha();
        //miVentana.setVisible(true);

    }

    static void chicha() {
        JPanel miPanel = new JPanel();
        miPanel.setLayout(new BoxLayout(miPanel, BoxLayout.PAGE_AXIS));
        miPanel.setBounds(20, 20, 350, 450);
        miPanel.setBackground(Color.CYAN);

        JLabel lUsuario = new JLabel("Usuario:");
        lUsuario.setVisible(true);
        JTextField campoUsuario = new JTextField();
        JLabel lPwd = new JLabel("Contraseña:");
        JPasswordField campoPwd = new JPasswordField();
        JButton bAcceso = new JButton("Acceder");

        miPanel.add(lUsuario);
        miPanel.add(campoUsuario);
        miPanel.add(lPwd);
        miPanel.add(campoPwd);
        miPanel.add(bAcceso);

        miPanel.setVisible(true);
        miVentana.add(miPanel);
    }
}

Components need to be added to the frame BEFORE the frame is made visible.

One of the functions of the setVisible() method is to invoke the layout manager. Otherwise components have a size() of (0, 0) so there is nothing to paint.

Also, all GUI components should be created on the Event Dispatch Thread (EDT) , otherwise you can have random results. Read the section from the Swing tutorial on Concurrency for more information.

Take a look at the FrameDemo from How to Make Frames for the most basic example of how your code should be structured to avoid problems. It shows how to create components on the EDT and how to make the frame visible.

they won't ve visible until I resize the window,

Resizing the frame will also cause the layout manager to be invoked.

miPanel.setBounds(20, 20, 350, 450);

That statement will do nothing because the layout manager of the frame will determine the size and location of the panel based on the rules of the layout manager. The default layout manager for a frame is a BorderLayout, so basically the panel will get all the space available to the frame.

The tutorial also has a section on Layout Managers that you should read.

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