JAVA pb avec GridBagLayout
Résolu/Fermé
apox
Messages postés
26
Date d'inscription
mardi 8 juin 2004
Statut
Membre
Dernière intervention
16 janvier 2006
-
13 nov. 2004 à 17:20
debutant123 - 16 avril 2009 à 18:38
debutant123 - 16 avril 2009 à 18:38
A voir également:
- JAVA pb avec GridBagLayout
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Waptrick java voiture - Télécharger - Jeux vidéo
- Java décompiler - Télécharger - Langages
2 réponses
Utilisateur anonyme
13 nov. 2004 à 20:50
13 nov. 2004 à 20:50
Bonjour,
il faut d'abord définir un gridbaglayout:
GridBagLayout g = new GridBagLayout();
Ensuite dire que le panneau l'utilisera:
Panel pane = new Panel();
pane.setLayout(g);
Ensuite on définit le gridbagconstraints
GridBagConstraints c = new GridBagConstraints();
Puis on ajoute les composants:
Jlabel label = new JLabel("Essai");
c.gridx = 0;
c.gridy = 0;
pane.add(label, c);
a+
il faut d'abord définir un gridbaglayout:
GridBagLayout g = new GridBagLayout();
Ensuite dire que le panneau l'utilisera:
Panel pane = new Panel();
pane.setLayout(g);
Ensuite on définit le gridbagconstraints
GridBagConstraints c = new GridBagConstraints();
Puis on ajoute les composants:
Jlabel label = new JLabel("Essai");
c.gridx = 0;
c.gridy = 0;
pane.add(label, c);
a+
16 avril 2009 à 18:38
je veux placer plusieurs composant de cette façon là:
https://www.sfr.fr/fermeture-des-pages-perso.html (dsl je ne sais pas insérer une image(si on peut))
donc voilà mon problème
je vous laisse mon code décomposé en 4classes une principale(fenêtre) une dessinant les leds une dessinant les afficheurs 7segments et une dessinant l'afficheur hexadécimal:
mais je ne pense pas que le problème vienne des autres classes que fenêtre, car lorsque je les ai testées seul elles fonctionnaient (je les ai donc mis juste pour info)
je n'ai également pas mis la classe main permettant de le lancer, mais c'est juste une instance de fenêtre.
Donc voilà si quelqu'un peut m'aider SVP, JE GALERE DEPUIS 5 JOUR DESSUS ARG!!!
merci
cdlt le débutant
public class Fenetre extends JFrame{
public Fenetre()
{
JFrame afficheurPan = new JFrame("test du gridbaglayout");
afficheurPan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//on crée le composant de 16 leds
Leds led = new Leds();
//on crée le composant afficheur 7 segment
Affichicheur7segments aff7seg = new Affichicheur7segments();
//on crée l'afficheur hexadecimal
AffHexaPan affHexa = new AffHexaPan();
//on crée les boutons et les indicateurs on/off
String activInter1 = "on";
String activInter2 = "on";
String activInter3 = "on";
String activInter4 = "on";
String activInter5 = "on";
String activInter6 = "on";
JButton inter1 = new JButton("1");
inter1.setPreferredSize(new Dimension(41,15));
JLabel activationInter1 = new JLabel(activInter1);
JButton inter2 = new JButton("2");
inter2.setPreferredSize(new Dimension(41,15));
JLabel activationInter2 = new JLabel(activInter2);
JButton inter3 = new JButton("3");
inter3.setPreferredSize(new Dimension(41,15));
JLabel activationInter3 = new JLabel(activInter3);
JButton inter4 = new JButton("4");
inter4.setPreferredSize(new Dimension(41,15));
JLabel activationInter4 = new JLabel(activInter4);
JButton inter5 = new JButton("5");
inter5.setPreferredSize(new Dimension(41,15));
JLabel activationInter5 = new JLabel(activInter5);
JButton inter6 = new JButton("6");
inter6.setPreferredSize(new Dimension(41,15));
JLabel activationInter6 = new JLabel(activInter6);
afficheurPan.setLayout(new GridBagLayout());
//on crée les contraintes associées au GridBagLayout
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;//premiere colonne
gbc.gridy = 0;//premiere ligne
gbc.gridheight = 1;//une seule cellule de haut
gbc.gridwidth = GridBagConstraints.REMAINDER;//c'est le seul element de la ligne
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(10,15,0,0);//marge de 15px à gauche et 10px au dessus
afficheurPan.add(led,gbc);//affichage des leds
//on passe à la mise en place des interrupteurs et de leurs indicateurs
gbc.gridy = 1;//deuxieme ligne
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(1,1,1,1);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 0;
afficheurPan.add(inter1,gbc);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(1,1,1,1);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 1;
afficheurPan.add(inter2,gbc);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(1,1,1,1);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 2;
afficheurPan.add(inter3,gbc);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(1,1,1,1);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 3;
afficheurPan.add(inter4,gbc);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(1,1,1,1);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 4;
afficheurPan.add(inter5,gbc);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(1,1,1,1);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 5;
afficheurPan.add(inter6,gbc);
gbc.gridy = 2;//troisieme ligne
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(1,1,1,1);//petite marge autour du composant
gbc.gridheight = 1;//hauteur de une seule cellule
gbc.gridwidth = 1;
gbc.gridx = 0;//1ere colonne
afficheurPan.add(activationInter1,gbc);
gbc.gridx = 1;//2eme colonne
afficheurPan.add(activationInter2,gbc);
gbc.gridx = 2;//3eme colonne
afficheurPan.add(activationInter3,gbc);
gbc.gridx = 3;//4eme colonne
afficheurPan.add(activationInter4,gbc);
gbc.gridx = 4;//5eme colonne
afficheurPan.add(activationInter5,gbc);
gbc.gridx = 5;//6eme colonne
afficheurPan.add(activationInter6,gbc);
//mise en place des afficheurs
gbc.gridx = 0;//1ere colonne
gbc.gridy = 3;//4eme ligne
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.insets = new Insets(0,0,0,0);
afficheurPan.add(aff7seg,gbc);
gbc.gridx = 1;//2eme colonne
gbc.gridy = 3;//4eme ligne
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;//c'est le seul element de la ligne
gbc.insets = new Insets(0,0,0,0);
afficheurPan.add(affHexa,gbc);
afficheurPan.setMinimumSize(new Dimension(620, 300));
afficheurPan.setLocationRelativeTo(null);
afficheurPan.setVisible(true);
}
}
public class Leds extends JPanel{
private static final long serialVersionUID = 1;
/***************************** VARIABLES GLOBALES DE LA CLASSE ***********************************/
//Tableau qui sélectionnera les leds à allumer
static Color [] color = new Color[16];
/**Constructeur de la classe LedsPan
*
*/
/****************************************** CONSTRUCTEUR******************************************/
public Leds(){
setPreferredSize(new Dimension(200,60));
for(int i=0;i<16;i++)color[i] = Color.pink;
}
/************************************** METHODE PAINTCOMPONENT **********************************/
@Override
public void paintComponent(Graphics g){
int x=10;
int y=10;
for(int i=0;i<16;i++){
if((i%4) == 0){x+=25;}
if(i!=0)x+=20;
/**
* Dessin d'une led
*/
g.setColor(color[i]);
int xl=x;
int yl=y;
g.fillOval(xl,yl,15,15);
}
}
}
public class Affichicheur7segments extends JPanel{
private static final long serialVersionUID = 1;
/***************************** VARIABLES GLOBALES DE LA CLASSE ***********************************/
//Tableau qui sélectionnera les afficheurs actifs (ici valeurs par défaut)
int [] TabAff = new int [4];
//Tableau qui sélectionnera les segments à allumer (ici valeurs par défaut)
static Color [] color = new Color[8];
/**
* Constructeur de la classe Aff7SegPan
*/
/****************************************** CONSTRUCTEUR******************************************/
public Affichicheur7segments(){
setPreferredSize(new Dimension(270,130));
for(int i=0;i<4;i++)TabAff[i] = 1;
for(int i=0;i<8;i++)color[i] = Color.lightGray;
}
/************************************** METHODE PAINTCOMPONENT **********************************/
@Override
public void paintComponent(Graphics g)
{
int x=0;
int y=0;
for(int i=1;i<5;i++)
{
x=0+65*(i-1);
/**
* Dessin du segment a
*/
if (TabAff[i-1]==0)g.setColor(color[0]);
else g.setColor(Color.lightGray);
int xa[]={53+x,48+x,18+x,13+x,18+x,48+x};
int ya[]={5+y,0+y,0+y,5+y,10+y,10+y};
Polygon segmenta = new Polygon(xa,ya,6);
g.fillPolygon(segmenta);
/**
* Dessin du segment b
*/
if (TabAff[i-1]==0) g.setColor(color[1]);
else g.setColor(Color.lightGray);
int xb[]={55+x,60+x,60+x,55+x,50+x,50+x};
int yb[]={7+y,12+y,42+y,47+y,42+y,12+y};
Polygon segmentb = new Polygon(xb,yb,6);
g.fillPolygon(segmentb);
/**
* Dessin du segment c
*/
if (TabAff[i-1]==0) g.setColor(color[2]);
else g.setColor(Color.lightGray);
int xc[]={55+x,60+x,60+x,55+x,50+x,50+x};
int yc[]={52+y,57+y,87+y,92+y,87+y,57+y};
Polygon segmentc = new Polygon(xc,yc,6);
g.fillPolygon(segmentc);
/**
* Dessin du segment d
*/
if (TabAff[i-1]==0) g.setColor(color[3]);
else g.setColor(Color.lightGray);
int xd[]={53+x,48+x,18+x,13+x,18+x,48+x};
int yd[]={95+y,100+y,100+y,95+y,90+y,90+y};
Polygon segmentd = new Polygon(xd,yd,6);
g.fillPolygon(segmentd);
/**
* Dessin du segment e
*/
if (TabAff[i-1]==0) g.setColor(color[4]);
else g.setColor(Color.lightGray);
int xe[]={12+x,17+x,17+x,12+x,7+x,7+x};
int ye[]={52+y,57+y,87+y,92+y,87+y,57+y};
Polygon segmente = new Polygon(xe,ye,6);
g.fillPolygon(segmente);
/**
* Dessin du segment f
*/
if (TabAff[i-1]==0) g.setColor(color[5]);
else g.setColor(Color.lightGray);
int xf[]={12+x,17+x,17+x,12+x,7+x,7+x};
int yf[]={7+y,12+y,42+y,47+y,42+y,12+y};
Polygon segmentf = new Polygon(xf,yf,6);
g.fillPolygon(segmentf);
/**
* Dessin du segment g
*/
if (TabAff[i-1]==0) g.setColor(color[6]);
else g.setColor(Color.lightGray);
int xg[]={53+x,48+x,18+x,13+x,18+x,48+x};
int yg[]={50+y,55+y,55+y,50+y,45+y,45+y};
Polygon segmentg = new Polygon(xg,yg,6);
g.fillPolygon(segmentg);
/**
* Dessin du point p
*/
if (TabAff[i-1]==0) g.setColor(color[7]);
else g.setColor(Color.lightGray);
int xp=60+x;
int yp=90+y;
g.fillOval(xp,yp,10,10);
}
}
}
public class AffHexaPan extends JPanel{
private static final long serialVersionUID = 1;
/***************************** VARIABLES GLOBALES DE LA CLASSE ***********************************/
//Tableau qui precisera si les afficheurs sont actifs
boolean TabAff;
//Tableau qui sélectionnera les segments actifs
/** 0
* --
* 5|__|1
* 4| 6|2
* --
* 3
*/
//Tableau qui sélectionnera les segments à allumer
static Color [][] color = new Color[4][7];
int tabAff[] = new int[4];
/**
* Constructeur de la classe AffHexaPan
*/
/****************************************** CONSTRUCTEUR******************************************/
public AffHexaPan()
{
setPreferredSize(new Dimension(100,130));
setLocation(20,20);
TabAff = false;
for(int j=0;j<4;j++)
{
for(int i=0;i<7;i++)color[j][i] = Color.lightGray;
}
}
/************************************** METHODE PAINTCOMPONENT **********************************/
public void paintComponent(Graphics g)
{
int xd = 0;
int xpl = 30;
int ypl = 30;
int x = 0;
int y = 0;
for(int i=1;i<5;i++){
xd=50*(i-1);
/**
* Dessin des 5 points du haut a gauche
*/
if (TabAff) g.setColor(color[i-1][5]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 0 + xd + xpl;
y = 8*(j-1) + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* Dessin des 5 points du haut a droite
*/
if (TabAff) g.setColor(color[i-1][1]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 8*4 + xd + xpl;
y = 8*(j-1) + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* Dessin des 5 points du bas a gauche
*/
if (TabAff) g.setColor(color[i-1][4]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 0 + xd + xpl;
y = 8*4+8*(j-1) + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* Dessin des 5 points du bas a droite
*/
if (TabAff) g.setColor(color[i-1][2]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 8*4 + xd + xpl;
y = 8*4+8*(j-1) + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* Dessin des 5 points du bas
*/
if (TabAff) g.setColor(color[i-1][3]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 8*(j-1) + xd + xpl;
y = 8*4*2 + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* Dessin des 5 points du milieu
*/
if (TabAff) g.setColor(color[i-1][6]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 8*(j-1) + xd + xpl;
y = 8*4 + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* Dessin des 5 points du haut
*/
if (TabAff) g.setColor(color[i-1][0]);
else g.setColor(Color.lightGray);
for(int j=1;j<6;j++){
x = 8*(j-1) + xd + xpl;
y = 0 + ypl;
/**
* Dessin d un point
*/
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
/**
* On redessine les bords
*/
if(TabAff){
if(color[i-1][0] == Color.red){
g.setColor(color[i-1][0]);
for(int j=1;j<6;j++){
x = 8*(j-1) + xd + xpl;
y = 0 + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
if(color[i-1][1] == Color.red){
g.setColor(color[i-1][1]);
for(int j=1;j<6;j++){
x = 8*4 + xd + xpl;
y = 8*(j-1) + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
if(color[i-1][2] == Color.red){
g.setColor(color[i-1][2]);
for(int j=1;j<6;j++){
x = 8*4 + xd + xpl;
y = 8*4+8*(j-1) + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
if(color[i-1][3] == Color.red){
g.setColor(color[i-1][3]);
for(int j=1;j<6;j++){
x = 8*(j-1) + xd + xpl;
y = 8*4*2 + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
if(color[i-1][4] == Color.red){
g.setColor(color[i-1][4]);
for(int j=1;j<6;j++){
x = 0 + xd + xpl;
y = 8*4+8*(j-1) + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
if(color[i-1][5] == Color.red){
g.setColor(color[i-1][5]);
for(int j=1;j<6;j++){
x = 0 + xd + xpl;
y = 8*(j-1) + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
if(color[i-1][6] == Color.red){
g.setColor(color[i-1][6]);
for(int j=1;j<6;j++){
x = 8*(j-1) + xd + xpl;
y = 8*4 + ypl;
int xp=x;
int yp=y;
g.fillOval(xp,yp,6,6);
}
}
}
}
}
}