Why does not the object appear button1 in the Frame?

0
package bienvenidos;
import javax.swing.*;
import java.awt.*;

    public class CapturaDatos {

        public static void main(String[] args) {
            PantallaCaptura pantalla1=new PantallaCaptura();
            pantalla1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

    class PantallaCaptura extends JFrame{
            public PantallaCaptura() {
            setTitle("Pantalla de Captura");
                setBounds(400,200,400,400);
                setVisible(true);
                add(new LaminaCaptura());
            }   
    }

     class LaminaCaptura extends JPanel{

        public LaminaCaptura(){
         boton1=new JButton("CapturarDato");
         add(boton1);
         }

         JButton boton1;
     }
    
asked by Carlos Morfin Diaz 13.10.2018 в 03:18
source

1 answer

1

The JFrame PantallaCaptura first becomes visible and then the container with the JButton component is added, but as there is no 'refresh' of the GUI then it is not possible to see what is added (unless the JFrame is re-dimensioned).

What can be done is:

setVisible(true);
add(new LaminaCaptura());
revalidate(); //actualiza el árbol de componentes (GUI) y llama a repaint()

Or on the other hand, simply put components first and then make the JFrame visible:

add(new LaminaCaptura());
setVisible(true);
    
answered by 13.10.2018 / 04:54
source