How can I put a JPanel class in a JIntercalFrame?

1

I have created several JPanel classes and now I want to show them in a JDesktopPane, but in the JDesktopPane you can not open a JPanel, can you? So it occurs to me to call the JPanel class from a JInternalFrame and then call the JInternalFrame to display it in JDesktopPane. Please, I need help.

    
asked by Neftalí García 07.12.2018 в 17:19
source

1 answer

0

The problem I see so that the JInternalFrame is not shown is that you have not set a size, otherwise you will have a default size of 0.0

I'll give you an example of how you could do it.

public class PruebaInternalFrame {

    public static void main(String[] args) {
        new PruebaInternalFrame();
    }

    /**
     * Crea el JFrame, el JDesktopPane, un JInternalFrame 
     * de muestra y lo visualiza.
     */
    public PruebaInternalFrame() {
        // El JFrame con el JDesktopPane
        JFrame v = new JFrame("Prueba JInternalFrame");
        JDesktopPane dp = new JDesktopPane();
        v.getContentPane().add(dp);

        // Se construye el panel que irá dentro del JInternalFrame
        JPanel p = new JPanel();
        p.add(new JLabel("Etiqueta"));
        p.add(new JTextField(10));

        // Se construye el JInternalFrame
        JInternalFrame internal = new JInternalFrame("Un Internal Frame");
        internal.add(p);

        // Es importante darle tamaño -pack()- al JInternalFrame,
        // porque si no, tendrá tamaño 0,0 y no lo veremos.
        //internal.pack();
        internal.setSize(300, 200);

        // Por defecto el JInternalFrame no es redimensionable
        // tiene el botón de cerrar, así que se lo ponemos.
        internal.setResizable(true);
        internal.setClosable(true);

        // Se mete el internal en el JDesktopPane
        dp.add(internal);

        // Se visualiza todo.
        v.setBounds(200,200, 500,500);
        v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        v.setVisible(true);

        // Se visualiza el JInternalFrame
        internal.setVisible(true);
    }

}

Result:

Source: Simple example with JInternalFrame

    
answered by 07.12.2018 / 18:45
source