How is a panel inside a container framed?

0

I have the frame main% and a class panel , what I want is to make several classes panel and go putting them in the box. The frame main% has a panel already in series and would change. My problem is that everything is going well but the container (red background) does not fit the panel (green).

This is my code:

{
    MenuOperaciones Menu = new MenuOperaciones();
    contenedor = panel0;
    contenedor.removeAll();
    contenedor.setBackground(Color.red);
    Menu.setSize(panel0.getSize());
    Menu.setBounds(panel0.getBounds());

    contenedor.add(Menu);
    contenedor.repaint();
    contenedor.validate();
}

    
asked by Sergio Muñoz Garcia 21.05.2018 в 20:20
source

1 answer

0

I'll give you an example to use it with java.awt.CardLayout.

JPanel contenedor = new JPanel(new CardLayout());
JPanel menu1 = new JPanel(); // configuramos este panel
JPanel menu2 = new JPanel(); //configuramos...

contenedor.add(menu1, "menu1");
contenedor.add(menu2, "menu2");

Here you are adding the different panels that you can show in the container, and to select one, or to show one, you need to specify which one you want to show. You do it in the show method, which I put below:

then where you listen to the buttons:

public void actionPerformed(java.awt.event.ActionEvent e) {
    ....
    String menuSelectionado = ?? // menu1 o menu2 por ejemplo       

    java.awt.CardLayout layout = (java.awt.CardLayout)contenedor.getLayout());
    layout.show(contenedor, menuSeleccionado); //aquí especificas cual quieres mostrar en el contenedor.

}

    
answered by 21.05.2018 / 23:50
source