0

I begin in the Java programming language. I would like to make a JFrame and put a background image on it and above the JFrame, I would like to insert widgets : JTextArea , JButton ... but they can not overlap. Here is my code :

public void go() {
    cadre = new JFrame("Premiere Feneêtre");
    cadre.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    cadre.setSize(600, 600);
    cadre.setVisible(true);`

    BorderLayout agencement = new BorderLayout();
    JPanel arrierePlan = new JPanel(agencement);
    arrierePlan.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    Box boiteABoutons = new Box(BoxLayout.PAGE_AXIS);
    boiteABoutons.setSize(150,150);
    boiteABoutons.setOpaque(false);
    //boiteABoutons.setBackground(Color.yellow);
    b1 = new JButton("Bouton 1");
    b1.addActionListener(this);
    b2 = new JButton("Bouton 2");
    b2.addActionListener(this);
    b3 = new JButton("Bouton 3");
    boiteABoutons.add(b1);
    boiteABoutons.add(b2);
    boiteABoutons.add(b3);
    zoneTexte = new JTextArea();

    Panneau p1 = new Panneau();
    arrierePlan.add(BorderLayout.SOUTH, boiteABoutons);
    arrierePlan.add(BorderLayout.NORTH,zoneTexte);

    cadre.getContentPane().add(p1);
    cadre.getContentPane().add(arrierePlan);

}

public class Panneau extends JPanel {
    public void paintComponent(Graphics g){
        try {
            Image img = ImageIO.read(new File("C:/Users/........png"));
            //g.drawImage(img, 10, 10, this);
            g.drawImage(img, 10, 10, this.getWidth(), this.getHeight(), this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I've read several things here regarding this problem I encounter, but nothing really helped me .

Thank you Sorry for bad english

Ilya Rochev
  • 157
  • 1
  • 3
  • 13
Manimalis
  • 173
  • 1
  • 1
  • 11

1 Answers1

1
cadre.getContentPane().add(p1);
cadre.getContentPane().add(arrierePlan);

You can't add multiple component to the CENTER of a BorderLayout.

Instead you want to add the component to the image panel:

Panneau p1 = new Panneau();
p1.setLayout( new BorderLayout() );
p1.add(boiteABoutons, BorderLayout.SOUTH);
p1.add(zoneTexte, BorderLayout.NORTH);
cadre.add(p1);

In the future search the forum before asking a question (the topic title usually contains good keywords to start with). As you can see by the posting found under the Related heading on the right side of this page, this question is asked frequently.

camickr
  • 321,443
  • 19
  • 166
  • 288