javaswinglayout-manager

GridBagLayout in a BorderLayout problem : no display in BorderLayout.CENTER


I'm trying to add a GridBagLayout in the center of a BorderLayout, but when I add it to the center, it doesn't appear. Despite this, it appears correctly if I place it in another position in the BorderLayout. I tried to place another Component in the center of the BorderLayout, but nothing appear.

I want to create multiple panels to display them in my JFrame. I'm trying to add these panels while I'm building them with the instance attribute in my JFrame.

Here is an example of what I want to get enter image description here

import java.awt.*;
import javax.swing.*;

public class Fenetre
{

  private JPanel contentPane;
  private PanelAccueil panelAccueil;

  public void fenetreGraphique()
  {
      //Définit une nouvelle fenêtre pour le GUI
      JFrame fenetre = new JFrame("MyFrame");
      fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      fenetre.setExtendedState(JFrame.MAXIMIZED_BOTH);
      fenetre.setLocationRelativeTo(null);

      //Définit les panels qui vont pouvoir s'afficher dans la fenêtre
      contentPane = new JPanel();

      //Ajout des différents panels créés
      panelAccueil = new PanelAccueil(contentPane);
      contentPane.add(panelAccueil);

      //Affichage de la fenêtre
      fenetre.setContentPane(contentPane);
      fenetre.pack();
      fenetre.setVisible(true);
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new Fenetre().fenetreGraphique();
        }
    });
  }
}

class PanelAccueil extends JPanel
{
    private JPanel panelAccueil;
    private JLabel messageAccueil;
    private JLabel signature;
    private JButton button1;
    private JButton button2;

    public PanelAccueil(JPanel panel){

        panelAccueil = panel;
        panelAccueil.setBackground(new Color(51,51,51));
        panelAccueil.setLayout( new BorderLayout() );

        //Premier panel pour le bandeau en haut
        messageAccueil = new JLabel("My Personal App", SwingConstants.CENTER);
        messageAccueil.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        messageAccueil.setForeground( new Color(255,255,255) );
        messageAccueil.setFont(new Font("Tahoma", Font.PLAIN, 36));

        //Ajout des boutons du menu
        JPanel boutons = new JPanel();
        boutons.setBackground( new Color(51,51,51) );
        boutons.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;

        button1 = new JButton("Button1");
        button1.setPreferredSize( new Dimension(300, 60) );
        boutons.add(button1, gbc);

        gbc.gridy = 1;
        button2 = new JButton("Button2");
        button2.setPreferredSize( new Dimension(300, 60) );
        boutons.add(button2, gbc);


        //Dernier panel pour la signature en bas à droite
        signature = new JLabel("Developed by me", SwingConstants.RIGHT);
        signature.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        signature.setForeground( new Color(255,255,255) );
        signature.setFont(new Font("Tahoma", Font.PLAIN, 15));

        panelAccueil.add(messageAccueil, BorderLayout.NORTH);
        panelAccueil.add(boutons, BorderLayout.CENTER);
        panelAccueil.add(signature, BorderLayout.SOUTH);
  }
}

Solution

  • There were a number of errors in the code, but it mostly came down to confusion about the content pane (which the code was adding to another panel).

    enter image description here

    Many changes were made to this code, examine it closely:

    import java.awt.*;
    import javax.swing.*;
    
    public class Fenetre {
    
        private PanelAccueil panelAccueil;
    
        public void fenetreGraphique() {
            //Définit une nouvelle fenêtre pour le GUI
            JFrame fenetre = new JFrame("MyFrame");
            fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            fenetre.setExtendedState(JFrame.MAXIMIZED_BOTH);
            fenetre.setLocationRelativeTo(null);
    
            //Définit les panels qui vont pouvoir s'afficher dans la fenêtre
            JPanel contentPane = new JPanel();
    
            //Ajout des différents panels créés
            panelAccueil = new PanelAccueil(contentPane);
            //contentPane.add(panelAccueil.panelAccueil);
    
            //Affichage de la fenêtre
            fenetre.setContentPane(panelAccueil.panelAccueil);
            fenetre.pack();
            fenetre.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Fenetre()::fenetreGraphique);
        }
    }
    
    class PanelAccueil {
        
        JPanel panelAccueil;
    
        public PanelAccueil(JPanel panel) {
            panelAccueil = panel;
            panelAccueil.setBackground(new Color(51, 51, 51));
            panelAccueil.setLayout(new BorderLayout());
    
            //Premier panel pour le bandeau en haut
            JLabel messageAccueil = new JLabel("My Personal App", SwingConstants.CENTER);
            messageAccueil.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            messageAccueil.setForeground(new Color(255, 255, 255));
            messageAccueil.setFont(new Font("Tahoma", Font.PLAIN, 36));
    
            //Ajout des boutons du menu
            JPanel boutons = new JPanel();
            boutons.setBackground(Color.red);
            boutons.setBackground(new Color(51, 51, 51));
            boutons.setLayout(new GridBagLayout());
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.insets = new Insets(20,20,20,20);
    
            Insets buttonInsets = new Insets(10,100,10,100);
            for (int ii=0; ii<6; ii++) {
                JButton b = new JButton("Button " + (ii+1));
                b.setFont(b.getFont().deriveFont(30f));
                b.setMargin(buttonInsets);
                gbc.gridy = ii;
                boutons.add(b, gbc);
            }
    
            //Dernier panel pour la signature en bas à droite
            JLabel signature = new JLabel("Developed by me", SwingConstants.RIGHT);
            signature.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            signature.setForeground(new Color(255, 255, 255));
            signature.setFont(new Font("Tahoma", Font.PLAIN, 15));
    
            panelAccueil.add(messageAccueil, BorderLayout.NORTH);
            panelAccueil.add(boutons, BorderLayout.CENTER);
            panelAccueil.add(signature, BorderLayout.SOUTH);
        }
    }