IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaces Graphiques en Java Discussion :

Problème placement composant GridBagLayout


Sujet :

Interfaces Graphiques en Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 4
    Par défaut Problème placement composant GridBagLayout
    Bonjour, j'ai un petit problème graphique que je n'arrive pas à résoudre.
    je veux placer plusieurs composant de cette façon là:


    et j'ai plutôt ça!


    donc voilà mon problème je ne comprend pas pourquoi certains de mes composants se coupent, et pourquoi mes boutons se décalent à droite.
    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

    voici la classe Fenetre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
     
    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);
        }
    }
    la classe Leds permettant de creer les leds (les points rouges)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
     
    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);
    		}
    	}
     
     
    }
    voici la classe permettant de créer l'afficheur 7 segments
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
     
    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);
    		}
    	}
    }
    la classe permettant de créer l'afficheur hexadecimal
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
     
    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);
    					}
    				}
    			}
    		}
    	}
    }
    voilà, en espérant que vous pourrez m'aider, je suis à l'écoute de toutes les idées!! même si il faut tout refaire.

  2. #2
    Membre éclairé Avatar de Balbuzard
    Profil pro
    Inscrit en
    Août 2008
    Messages
    381
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 381
    Par défaut
    Sans vouloir casser tout ton travail, je trouve GridBagLayout super lourd, il existe pas mal d'autres composants qui peuvent te permettre de faire la même chose sans te prendre autant la tête. Personnellement, j'ai essayé avec succès TableLayout. Cette librairie n'appartient pas à la librairie standard, il te faut donc télécharger le jar correspondant ICI, ensuite ajoutes-le à ton projet. (Sous Eclipse, clic droit sur ton projet à gauche, puis build path, add external JAR, et le chemin vers ton Jar que je te conseille de mettre dans ton Workspace)

    La philosophie de TableLayout est de diviser ton Panel en cases comme un tableau et de les remplir ensuite avec une methode add(ligne, colonne, hauteur, largeur).
    Le tutoriel qui te permettra de tout comprendre: ICI

    Bien sûr, si quelqu'un parvient à te donner un coup de main en t'expliquant pourquoi ton GridBagLayout ne produit pas les effets escomptés, je suis aussi intéressé, je n'y jamais pu faire fonctionner ce LayoutManager correctement!

  3. #3
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 276
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    Déjà, les prefferedSize de tes objets Leds et AffHexaPan sont trop petites, c'est pourquoi on ne voit pas tout le composant.
    Ensuite, il faut voir que le GBL fonctionne avec un système de cellule.
    Quand tu places ton bouton 1, même s'il est petit, il occupe tout une cellule, comme le composant du dessus. Il est donc normal que ton bouton 2, soit placé en cellule x =2, juste après le plus grand composant de la colonne x=1.

    C'est à peu près clair ?

    J'ai modifié un peu tout ça à la va vite pour que cela se rapproche de ce que tu veux, mais il a moyen de faire plus simple je pense sans GBL.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
     
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    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);
            activationInter1.setPreferredSize(new Dimension(41, 15));
            JButton inter2 = new JButton("2");
            inter2.setPreferredSize(new Dimension(41,15));
            JLabel activationInter2 = new JLabel(activInter2);
            activationInter2.setPreferredSize(new Dimension(41, 15));
            JButton inter3 = new JButton("3");
            inter3.setPreferredSize(new Dimension(41,15));
            JLabel activationInter3 = new JLabel(activInter3);
            activationInter3.setPreferredSize(new Dimension(41, 15));
            JButton inter4 = new JButton("4");
            inter4.setPreferredSize(new Dimension(41,15));
            JLabel activationInter4 = new JLabel(activInter4);
            activationInter4.setPreferredSize(new Dimension(41, 15));
            JButton inter5 = new JButton("5");
            inter5.setPreferredSize(new Dimension(41,15));
            JLabel activationInter5 = new JLabel(activInter5);
            activationInter5.setPreferredSize(new Dimension(41, 15));
            JButton inter6 = new JButton("6");
            inter6.setPreferredSize(new Dimension(41,15));
            JLabel activationInter6 = new JLabel(activInter6);
            activationInter6.setPreferredSize(new Dimension(41, 15));
     
            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.gridwidth = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.NONE;
            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
     
            JPanel panelBoutons = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
            //gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.insets = new Insets(1,1,1,1);
            gbc.gridheight = 1;
            gbc.gridwidth = 1;
            gbc.gridx = 0;
            gbc.weighty = 0;
            //afficheurPan.add(inter1,gbc);
            panelBoutons.add(inter1);
            afficheurPan.add(panelBoutons,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);
            panelBoutons.add(inter2);
     
            //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);
            panelBoutons.add(inter3);
     
            //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);
            panelBoutons.add(inter4);
     
            //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);
            panelBoutons.add(inter5);
     
            //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);
            panelBoutons.add(inter6);
     
            JPanel panelLabels = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
            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);
            panelLabels.add(activationInter1);
            afficheurPan.add(panelLabels,gbc);
            gbc.gridx = 1;//2eme colonne
            //afficheurPan.add(activationInter2,gbc);
            panelLabels.add(activationInter2);
            gbc.gridx = 2;//3eme colonne
            //afficheurPan.add(activationInter3,gbc);
            panelLabels.add(activationInter3);
            gbc.gridx = 3;//4eme colonne
            //afficheurPan.add(activationInter4,gbc);
            panelLabels.add(activationInter4);
            gbc.gridx = 4;//5eme colonne
            //afficheurPan.add(activationInter5,gbc);
            panelLabels.add(activationInter5);
            gbc.gridx = 5;//6eme colonne
            //afficheurPan.add(activationInter6,gbc);
            panelLabels.add(activationInter6);
     
            JPanel panelAfficheur = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
            //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.FIRST_LINE_END;
            gbc.insets = new Insets(0,0,0,0);
            //afficheurPan.add(aff7seg,gbc);
            panelAfficheur.add(aff7seg);
            afficheurPan.add(panelAfficheur,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);
            panelAfficheur.add(affHexa);
     
            //afficheurPan.setMinimumSize(new Dimension(620, 300));
            afficheurPan.setLocationRelativeTo(null);
            afficheurPan.pack();
            afficheurPan.setVisible(true);
        }
     
        public static void main(String[] args) {
        	Fenetre f = new Fenetre();
        	f.setSize(400, 300);
        }
    }

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 4
    Par défaut merci
    merci j'ai réussi, il est vrai que la principal cause était la taille de mes composant et après plusieurs heure (hier soir et ce matin) j'ai fini entièrement mon projet.
    Merci pour ton aide, et à la prochaine

Discussions similaires

  1. Problème taille composant GridBagLayout
    Par alex148 dans le forum Agents de placement/Fenêtres
    Réponses: 1
    Dernier message: 15/05/2012, 08h26
  2. [ENVOI D'EMAIL] Problème de composant CDONTS
    Par ybenmakh dans le forum ASP
    Réponses: 1
    Dernier message: 10/01/2006, 15h43
  3. Problème de composant
    Par leycho dans le forum C++Builder
    Réponses: 7
    Dernier message: 03/01/2006, 16h04
  4. [C#]Problème sur composant perso (Bouton Transparent)
    Par SLE dans le forum Windows Forms
    Réponses: 1
    Dernier message: 06/10/2005, 00h12
  5. [VB.NET] Problème sur composant comboBox hérité
    Par SergeF dans le forum Windows Forms
    Réponses: 3
    Dernier message: 08/06/2004, 14h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo