Lier un JPanel à un JFrame (setContentPan)

Résolu/Fermé
Qoodsy - 8 mars 2016 à 14:59
 Qoodsy - 14 mars 2016 à 14:41
Bonjour, je chercher à afficher ma classe EcranDepart qui est un JPanel dans ma classe Fenetre qui est une JFrame.

Voici le code :
public class Fenetre extends JFrame {

private EcranDepart dep = new EcranDepart();

public static void main(String[] args) {
Fenetre f = new Fenetre();
}

public Fenetre() {
this.setTitle("Fenetre");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(dep);
this.setVisible(true);
}
}


class EcranDepart extends JPanel {

public JPanel pan;
public JPanel pantop;
public JPanel panmid;
public JPanel panbot;

public JButton connect;
public JButton sub;
public JButton info;

public EcranDepart() {
pan = new JPanel();
pan.setLayout(new BorderLayout());

pantop = new JPanel();
pantop.setPreferredSize(new Dimension(800, 0));
pan.add(pantop, BorderLayout.NORTH);

panmid = new JPanel();
panmid.setBackground(Color.red);
pan.add(panmid, BorderLayout.CENTER);

connect = new JButton("Se connecter");
connect.setPreferredSize(new Dimension(400, 100));
sub = new JButton("S'incrire");
sub.setPreferredSize(new Dimension(400, 100));
panmid.add(connect);
panmid.add(sub);

panbot = new JPanel();
panbot.setBackground(Color.white);
panbot.setPreferredSize(new Dimension(800, 50));
panbot.setLayout(new BorderLayout());
pan.add(panbot, BorderLayout.SOUTH);

info = new JButton("i");
info.setPreferredSize(new Dimension(50,50));
panbot.add(info, BorderLayout.EAST);
}
}


Merci d'avance :)

3 réponses

Utilisateur anonyme
8 mars 2016 à 19:12
Salut,

Il y a un problème ? A priori, je ne vois pas de problème ...
0
greg6614 Messages postés 592 Date d'inscription vendredi 7 août 2009 Statut Membre Dernière intervention 3 juin 2017 107
12 mars 2016 à 12:18
Salut, essai peut être
this.getContentPane().add(dep);

0
C'est bon j'ai trouvé le problème, j'avais créé un JPanel pan dans ma classe EcranDepart, du coup c'est lui qui prenait l'ascendant sur le contenu d'EcranDepart :)

Merci les gars
0