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

AWT/Swing Java Discussion :

GridBagLayout [Trucs & Astuces]


Sujet :

AWT/Swing Java

  1. #21
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Trés bien merci Calo, je voulais te demander aussi est-il possible de placer un composant dans une celulle de la facon qu'on veut?par exemple tout à gauche de la celulle.
    Sinon je pense(enfin j'espère) avoir bien compris le système du gridbaglayout maintenant grâce à toi biensûr
    Pourquoi ca marche jamais?Vive le café!

  2. #22
    Membre confirmé Avatar de calogerogigante
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    602
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2003
    Messages : 602
    Points : 497
    Points
    497
    Par défaut
    Allez, j'suis en forme !
    Je suis dans ma phase tutoriel, là !


    J'ai fait un exemple détaillé sur lequel tu pourras te casser la tête tout le week-end, nicotine !
    (Naaan, je rigole, j'ai commenté un maximum).

    Dans cet exemple, tu verras 8 JPanels, imbriqués dans le GridBagLayout de la JFrame.
    J'y ai développé 3 JPanels avec 3 Layouts différents un BoxLayout, un FlowLayout et un GridBagLayout.
    Tout ça pour te montrer aussi comment tu peux imbriquer des GridBagLayout dans un GridBagLayout.

    Certains trouveront que ce code n'est pas lisible.
    Peut-être... Mais moi pas.
    Surtout quand on a fait soi-même le codage, et qu'on dispose du papier où l'on a tracé le quadrillage sur le schéma de l'interface.

    Autrement dit, il est impératif (pour celui qui veut étudier comment on dispose les composants dans les 2 GridBagLayout de mon dernier exemple) d'avoir ABSOLUMENT les schémas que je me suis fait chier à dessiner toute l'après-midi, pour que tout soit plus clair !!

    Bon, eh bien... Bon décortiquage de mon code !


    Bon week-end.

    P.S.: y'a encore deux schémas à attacher...

  3. #23
    Membre confirmé Avatar de calogerogigante
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    602
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2003
    Messages : 602
    Points : 497
    Points
    497
    Par défaut
    Et voici les deux fichiers java correspondants :

    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
    import javax.swing.UIManager;
     
    public class EssaiDeGridBagLayout3
    {
    public static void main (String args[])
       {
       try
         {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         }
       catch (Exception e)
          {
          System.out.println(e);
          }
       MaFenetre fen = new MaFenetre();
       fen.setVisible(true);
       }
    }
    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
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
     
    class MaFenetre extends JFrame
    {
    // paramètres de position des JPanels pan1 à pan8 dans la JFrame
    private int pan_x[]   = { 0, 4, 0, 1, 3,  0, 0, 2};
    private int pan_y[]   = { 0, 0, 1, 1, 1,  2, 3, 3};
    private int pan_larg[]= { 4, 1, 1, 2, 1,  5, 2, 3};
    private int pan_haut[]= { 1, 2, 1, 1, 1,  1, 1, 1};
    private int pan_px[]  = {70,50,20,25,25,120,35,85};
    private int pan_py[]  = {30,56,26,26,26, 21,14,14};
     
    // composants de pan2 :
    private JLabel     bidon_1 = new JLabel("      ");
    private JLabel      textl1 = new JLabel("ID  ");
    private JTextField  textf1 = new JTextField();
    private JLabel     bidon_2 = new JLabel(" ");
    private JLabel      textl2 = new JLabel("Nom  ");
    private JTextField  textf2 = new JTextField();
    private JLabel     bidon_3 = new JLabel("          ");
    private JLabel      textl3 = new JLabel("Prénom  ");
    private JTextField  textf3 = new JTextField();
    private JLabel     bidon_4 = new JLabel(" ");
    private JLabel      textl4 = new JLabel("Adresse  ");
    private JTextField  textf4 = new JTextField();
    private JLabel     bidon_5 = new JLabel(" ");
    private JLabel      textl5 = new JLabel("Age  ");
    private JTextField  textf5 = new JTextField();
    private JLabel     bidon_6 = new JLabel("      ");
     
    // position des composants de pan2 qui a un GridBagLayout
    // comme Layout :
    private int x[]   = { 0, 1, 2, 0, 1, 2, 2, 1, 2, 0, 1, 2, 0, 1, 2, 3};
    private int y[]   = { 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9,10};
    private int larg[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    private int haut[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    private int px[]  = { 5,20,20, 0,20,20,20,20,20, 5,20,20, 5,20,20, 5};
    private int py[]  = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
     
    // composants de pan3 :
    private Box boxVertical = Box.createVerticalBox();
    private JButton p3b1 = new JButton("Java");
    private JButton p3b2 = new JButton("Ca");
    private JButton p3b3 = new JButton("déchire");
     
    //composants de pan6 :
    private JButton p6b1 = new JButton("Bonjour");
    private JButton p6b2 = new JButton("Merci");
    private JButton p6b3 = new JButton("Et au revoir !");
     
    public MaFenetre()
      {
      setTitle("Exemple GridBagLayout");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
     
      //======================
      // élaboration de pan1 :
      //======================
      JPanel pan1 =new JPanel();  pan1.setBorder(new TitledBorder(" pan1 "));
      //======================
      // élaboration de pan2 :
      //======================
      JPanel pan2 =new JPanel();  pan2.setBorder(new TitledBorder(" pan2 "));
      GridBagLayout gpan2 = new GridBagLayout();
      pan2.setLayout(gpan2);
      GridBagConstraints c2 = new GridBagConstraints();
                         c2.fill = GridBagConstraints.BOTH ;
     
      for (int i=0; i<x.length; i++)
          {
          c2.gridx      = x[i];
          c2.gridy      = y[i];
          c2.gridwidth  = larg[i];
          c2.gridheight = haut[i];
          c2.weightx    = px[i];
          c2.weighty    = py[i];
          switch(i)
            {
            case  0: pan2.add( bidon_1, c2 ); break;
            case  1: pan2.add(  textl1, c2 ); break;
            case  2: pan2.add(  textf1, c2 ); break;
            case  3: pan2.add( bidon_2, c2 ); break;
            case  4: pan2.add(  textl2, c2 ); break;
            case  5: pan2.add(  textf2, c2 ); break;
            case  6: pan2.add( bidon_3, c2 ); break;
            case  7: pan2.add(  textl3, c2 ); break;
            case  8: pan2.add(  textf3, c2 ); break;
            case  9: pan2.add( bidon_4, c2 ); break;
            case 10: pan2.add(  textl4, c2 ); break;
            case 11: pan2.add(  textf4, c2 ); break;
            case 12: pan2.add( bidon_5, c2 ); break;
            case 13: pan2.add(  textl5, c2 ); break;
            case 14: pan2.add(  textf5, c2 ); break;
            case 15: pan2.add( bidon_6, c2 ); break;
            }
          }
     
      //======================
      // élaboration de pan3 :
      //======================
      JPanel pan3 =new JPanel();  pan3.setBorder(new TitledBorder(" pan3 "));
      boxVertical.add(p3b1);
      boxVertical.add(Box.createVerticalStrut(10));
      boxVertical.add(p3b2);
      boxVertical.add(Box.createVerticalStrut(10));
      boxVertical.add(p3b3);
      pan3.add(boxVertical);
      //======================
      // élaboration de pan4 :
      //======================
      JPanel pan4 =new JPanel();
      pan4.setBackground(Color.CYAN);
      //======================
      // élaboration de pan5 :
      //======================
      JPanel pan5 =new JPanel();  pan5.setBorder(new TitledBorder(" pan5 "));
      //======================
      // élaboration de pan6 :
      //======================
      JPanel pan6 =new JPanel();
      pan6.setBackground(Color.YELLOW);
      pan6.setPreferredSize(new Dimension(400,60));
      pan6.setLayout(new FlowLayout(FlowLayout.CENTER));
      pan6.add(p6b1);
      pan6.add(p6b2);
      pan6.add(p6b3);
      //======================
      // élaboration de pan7 :
      //======================
      JPanel pan7 =new JPanel();  pan7.setBorder(new TitledBorder(" pan7 "));
      //======================
      // élaboration de pan8 :
      //======================
      JPanel pan8 =new JPanel();  pan8.setBorder(new TitledBorder(" pan8 "));
     
     
      //==========================================================================
      // maintenant que les 8 Jpanels sont élaborés, on les place dans la JFrame :
      //==========================================================================
      Container contenu = getContentPane();
      GridBagLayout g = new GridBagLayout();
      contenu.setLayout(g);
      GridBagConstraints cf = new GridBagConstraints();
                         cf.fill = GridBagConstraints.BOTH ;
     
      for (int i=0; i<pan_x.length; i++)
          {
          cf.gridx      = pan_x[i];
          cf.gridy      = pan_y[i];
          cf.gridwidth  = pan_larg[i];
          cf.gridheight = pan_haut[i];
          cf.weightx    = pan_px[i];
          cf.weighty    = pan_py[i];
          switch(i)
            {
            case  0: contenu.add( pan1, cf ); break;
            case  1: contenu.add( pan2, cf ); break;
            case  2: contenu.add( pan3, cf ); break;
            case  3: contenu.add( pan4, cf ); break;
            case  4: contenu.add( pan5, cf ); break;
            case  5: contenu.add( pan6, cf ); break;
            case  6: contenu.add( pan7, cf ); break;
            case  7: contenu.add( pan8, cf ); break;
            }
          }
      pack();
      }
    }

  4. #24
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Ok merci beaucoup Calo,
    j'espère avoir enfin compris(enfin...).
    De plus moi je remplissais toute les cases vides par un JLabel, dans ta derniere image(avec les JTextfield), moi je mettais un JLabel en 0,0 1,0 2,0 3,0, alors que c'est vraiment idiot, il y a juste besoin d'en mettre un en 0,0 pour la largeur de la ligne.
    Je t'enverrai une image de mon soft quand j'aurais fini pour me dire ce que tu en pense.
    En tout cas un grand merci à toi
    Pourquoi ca marche jamais?Vive le café!

  5. #25
    Futur Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 19
    Points : 9
    Points
    9
    Par défaut merci!
    merci pour ce tutorial aussi génial que pédagogique!maintenant il sert de support de cour a tout ma classe

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. [GridBagLayout] Problème de dimension pour un JScrollPane
    Par cmoa59 dans le forum Agents de placement/Fenêtres
    Réponses: 5
    Dernier message: 26/07/2005, 12h58
  2. GridBagLayout, JTextField ou Choice,je ne sais pa
    Par tck-lt dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 20/07/2005, 19h24
  3. [UI]Aide sur les GridBagLayout
    Par leup dans le forum Agents de placement/Fenêtres
    Réponses: 8
    Dernier message: 19/05/2005, 18h35
  4. [Swing] probleme de GridBagLayout
    Par calypso dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 13/04/2005, 18h26
  5. [débutant]GridBagLayout
    Par pingoui dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 13/12/2004, 18h43

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