Problème avec Border Layout

Résolu
mar -  
 mar -
Bonjour,

j'ai un petit problème avec le Border Layout, j'ai définit 3 panels et je les ai inséré dans une fenêtre en la décomposant avec un Border Layout sur 3 zone , nord , ce,tre et sud mais les composants du nord et sud s'affiche à droit et à gache
voici mon code
this.add(titre ,BorderLayout.NORTH);
this.add(th ,BorderLayout.CENTER);
this.add(ccc , BorderLayout.SOUTH);

merci d'avance
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Il faudrait voir le reste de ton code, parce que ce ne sont pas ces 3 lignes qui posent problème...

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test
{
public static void main(String...args)
{
	JFrame frame = new JFrame();
			
	JPanel titre = new JPanel();
	titre.setBackground(Color.RED);
	frame.add(titre, BorderLayout.NORTH);
	
	JPanel th = new JPanel();
	th.setBackground(Color.GREEN);
	frame.add(th, BorderLayout.CENTER);
	
	JPanel ccc = new JPanel();
	ccc.setBackground(Color.BLUE);
	frame.add(ccc, BorderLayout.SOUTH);
	
	frame.setSize(200,200);
	frame.setVisible(true);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
0
mar
 
voici tout le code
package newpackage;

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

public class Form extends JPanel {
public Form(){
JTextField a = new JTextField("");
JTextField b = new JTextField("");
JTextField c = new JTextField("");
JTextField d = new JTextField("");
JTextField e = new JTextField("");
JTextField f = new JTextField("");
a.setPreferredSize(new Dimension(150, 30));
b.setPreferredSize(new Dimension(150, 30));
c.setPreferredSize(new Dimension(150, 30));
d.setPreferredSize(new Dimension(150, 30));
e.setPreferredSize(new Dimension(150, 30));
f.setPreferredSize(new Dimension(150, 30));

JLabel aa = new JLabel("Nom ");
JLabel bb = new JLabel("Prénom ");
JLabel cc = new JLabel("Mail ");
JLabel dd = new JLabel("Téléphone ");
JLabel ee = new JLabel("Login ");
JLabel ff = new JLabel("Mot de passe");
JLabel gg = new JLabel("Type ");
JLabel hh = new JLabel("Zone ");
JLabel titre = new JLabel(" Création d'un compte ");
titre.setSize(50,50);
titre.setBackground(Color.CYAN);
titre.setForeground(Color.red);
Font font = new Font("Arial",Font.BOLD,20);
titre.setFont(font);
Choice l = new Choice();
l.addItem("Nord");
l.addItem("Centre");
l.addItem("Sud");
CheckboxGroup t = new CheckboxGroup();
Checkbox y = new Checkbox("Super Administrateur",t,false);
Checkbox x = new Checkbox("Administrateur",t,true);
JPanel ii = new JPanel();
JPanel jj = new JPanel();
JPanel kk = new JPanel();
JPanel mm = new JPanel();
JPanel nn= new JPanel();
JPanel oo = new JPanel();
JPanel pp = new JPanel();
JPanel qq = new JPanel();
JPanel rr = new JPanel();
JPanel ss = new JPanel();
JPanel tt = new JPanel();
JPanel uu = new JPanel();
JPanel vv = new JPanel();
JPanel ww = new JPanel();
JPanel xx = new JPanel();
JPanel yy = new JPanel();
JPanel zz = new JPanel();
JPanel aaa = new JPanel();
JPanel bbb = new JPanel();
JPanel ccc = new JPanel();
JPanel zzz = new JPanel();
JPanel th = new JPanel();
JPanel abc = new JPanel();
JButton ok = new JButton("Valider");
xx.add(l);
pp.add(aa);
qq.add(bb);
rr.add(cc);
ss.add(dd);
tt.add(ee);
uu.add(ee);
ii.add(a);
jj.add(b);
kk.add(c);
mm.add(d);
nn.add(e);
oo.add(f);
vv.add(gg);
yy.add(y);
ww.add(hh);
zz.add(x);
ccc.add(ok);
tt.add(ff);

th.setLayout(new GridLayout(10,2));
th.add(aaa);
th.add(bbb);
th.add(pp);
th.add(ii);
th.add(qq);
th.add(jj);
th.add(rr);
th.add(kk);
th.add(ss);
th.add(mm);
th.add(uu);
th.add(oo);
th.add(tt);
th.add(nn);
th.add(vv);
th.add(zzz);
th.add(yy);
th.add(zz);
th.add(ww);
th.add(xx);

this.add(titre ,BorderLayout.NORTH);

this.add(th ,BorderLayout.CENTER);

this.add(ok , BorderLayout.SOUTH);

}
}
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
C'est parce que Form est un JPanel donc le layout par défaut est un FlowLayout (contrairement aux JFrame qui ont par défaut un BorderLayout). Tu devrais donc modifier le layout :

setLayout(new BorderLayout());
0
mar
 
merci bien :)
0