Sorry if duplicate, couldn't find a proper answer
I'm trying to center a JPanel which is in itself added into another JPanel
public class bgframe extends JPanel
{
// The Image to store the background image in.
Image img;
public bgframe()
{
// Loads the background image and stores in img object.
img = Toolkit.getDefaultToolkit().getImage("img/fondAccueil.png");
}
protected void paintComponent(Graphics g)
{
// Draws the img to the BackgroundPanel_accueil.
g.drawImage(img, 0, 0, this);
}
}
and
public class Frame_Accueil extends JFrame
{
public Frame_Accueil()
{
this.setSize(1000,500);
bgframe fond = new bgframe();
JPanel boutons = new JPanel(new GridBagLayout());
JButton btn1 = new JButton("btn1");
JButton btn2 = new JButton("btn1");
btn1.setPreferredSize(new Dimension(300, 50));
btn2.setPreferredSize(new Dimension(300, 50));
btn.add(btn1);
btn.add(btn2);
this.add(fond,BorderLayout.CENTER);
fond.add(boutons,BorderLayout.CENTER);
this.setVisible(true);
}
}
Problem is that I have the background but the buttons are not in the center. How can I fix this ??
Thanks