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 :

[SWING] GridBagLayout positionnement des composants


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    87
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 87
    Points : 55
    Points
    55
    Par défaut [SWING] GridBagLayout positionnement des composants
    Bonjour à tous!
    Ma demande va peut être vous paraître un peu insolite mais en fait je cherche à créer une interface en utilisant un GridBagLayout qui me permettrait d'obtenir une interface de cette forme là:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    +--------------------------------------------------+
    |                          1                       |
    +--------------------------------------------------+
    |           |             |     4     |            |
    |           |             |           |            |
    |     2     |       3     |-----------|   6        |
    |           |             |      5    |            |
    |           |             |           |            |
    +--------------------------------------------------+
    |           |                   8                  |
    |    7      |--------------------------------------|
    |           |                   9                  |
    +--------------------------------------------------+
    Quelqu'un aurait-il une solution sachant que je galère vraiment pour placer mes composants... j'utilise des gridx, gridy, gridwidth, gridheight... mais je n'arrive vraiment pas à faire mes deuxième et troisième lignes notamment!

    Merci d'avance et bonne journée à tous!

  2. #2
    Membre averti Avatar de biozaxx
    Profil pro
    Inscrit en
    Août 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 403
    Points : 375
    Points
    375
    Par défaut
    il faut que tu créés 5 lignes et 4 colonnes ! c'est ce que tu fais ?

  3. #3
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    87
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 87
    Points : 55
    Points
    55
    Par défaut
    Citation Envoyé par biozaxx
    il faut que tu créés 5 lignes et 4 colonnes ! c'est ce que tu fais ?
    Ben je sais pas exactement si c'est ce que je fais mais on peut déclarer la taille du GridBagLayout? On peut dire dès le départ qu'il va contenir 5 lignes et 4 colonnes? J'ai rien trouvé là-dessus!
    Je vous mets mon code dans 10 minutes...

  4. #4
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    87
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 87
    Points : 55
    Points
    55
    Par défaut
    Voici le code de ma fonction qui est chargée de créer mon JPanel principal:

    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
    private JPanel createPanneau(){
     
    		//declarations
    		JPanel panneau = new JPanel();
    		panneau.setBorder(new LineBorder(Color.black));
     
    		//installer le gestionnaire
    		 GridBagLayout g=new GridBagLayout();
    		 panneau.setLayout(g);
    		 //créer un objet de type GridBagConstraints
    		 GridBagConstraints c=new GridBagConstraints();
     
    		 //******************************************//
    		 c.fill=GridBagConstraints.NONE;
    		 c.weightx=1.0;
    		 c.gridheight=1; 
    		 c.gridwidth=GridBagConstraints.REMAINDER;
    		 c.gridx=0;
    		 c.gridy=0;
    		 c.anchor = GridBagConstraints.CENTER;
    		 JLabel monLabel1 = new JLabel("Bienvenue");
    		 monLabel1.setFont(new Font("Tahoma", Font.PLAIN, 25));
    		 panneau.add(monLabel1);
    		 g.setConstraints(monLabel1,c);
    		 //******************************************//
     
     
    		 //******************************************//		
    		 //réinitialisation
    		 c.weightx=0.0; 
    		 c.weighty=1.0;
    		 c.gridwidth=1;
    		 c.gridheight=2; //sur 2 lignes
    		 c.gridx=0;
    		 c.gridy=1;
    		 JLabel monLabel2 = new JLabel("Veuillez choisir les concepts que vous souhaitez mettre en évidence:");
    		 panneau.add(monLabel2); 
    		 g.setConstraints(monLabel2,c);
    		 //******************************************//		
     
    		 //******************************************//		
    		 //nouvelle réinitialisation
    		 c.fill=GridBagConstraints.NONE;
    		 //c.weighty=0.0; 
    		 //c.gridwidth=GridBagConstraints.RELATIVE;
    		 c.gridx=1;
    		 c.gridy=1;
    		 c.gridwidth=1;
    		 c.gridheight=2;
    		 String[] columnNames = {"Concepts"};
    		 Object[][] data = {
    				    {"Concept1"},
    				    {"Concept2"},
    				    {"Concept3"}};
    		 JTable maTable1 = new JTable(new MyTableModel(data, columnNames));
    		 maTable1.setShowGrid(false);
    		 JScrollPane scrollPane1 = new JScrollPane(maTable1);
    		 JPanel pannel1 = new JPanel();
    		 pannel1.add(scrollPane1);
    		 panneau.add(pannel1);
    		 g.setConstraints(pannel1,c);
    		 //******************************************//
     
    		 //******************************************//		 
    		 c.fill=GridBagConstraints.NONE;
    		 c.gridheight=1;
    		 c.gridwidth=1;
    		 c.gridheight=1;
    		 c.gridx=2;
    		 c.gridy=1;
    		 c.anchor=GridBagConstraints.SOUTH;
    		 JButton transfer1=new JButton(">>");
    		 panneau.add(transfer1);
    		 g.setConstraints(transfer1,c);
    		 //******************************************//		 
     
    		 //******************************************//		
    		 //nouvelle réinitialisation
    		 c.fill=GridBagConstraints.NONE;
    		 c.gridwidth=GridBagConstraints.REMAINDER;
    		 c.gridheight=2; //sur 2 lignes
    		 c.gridx=3;
    		 c.gridy=1;
    		 String[] columnNames2 = {"Concepts"};
    		 Object[][] data2 = {};
    		 JTable maTable2 = new JTable(new MyTableModel(data2, columnNames2));
    		 maTable2.setShowGrid(false);
    		 JScrollPane scrollPane2 = new JScrollPane(maTable2);
    		 JPanel pannel2 = new JPanel();
    		 pannel2.add(scrollPane2);
    		 panneau.add(pannel2);
    		 g.setConstraints(pannel2,c);
    		 //******************************************//
     
    		 //******************************************//		 
    		 c.fill=GridBagConstraints.NONE;
    		 c.gridheight=1;
    		 c.gridheight=1; 
    		 c.gridx=2;
    		 c.gridy=2;
    		 c.anchor=GridBagConstraints.NORTH;
    		 JButton transfer2=new JButton("<<");
    		 panneau.add(transfer2);
    		 g.setConstraints(transfer2,c);
    		 //******************************************//	
     
    		 //******************************************//		
    		 //réinitialisation
    		 c.weightx=0.0; 
    		 c.weighty=1.0;
    		 c.gridwidth=1;
    		 c.gridheight=2; //sur 2 lignes
    		 c.gridx=0;
    		 c.gridy=3;
    		 JLabel monLabel3 = new JLabel("Veuillez choisir la période souhaitée:");
    		 panneau.add(monLabel3); 
    		 g.setConstraints(monLabel3,c);
    		 //******************************************//	
     
     
    		 //******************************************//		
    		 c.fill=GridBagConstraints.NONE;
    		 c.gridheight=1;
    		 c.gridwidth=GridBagConstraints.REMAINDER;
    		 c.gridheight=1; //sur 2 lignes
    		 c.gridx=1;
    		 c.gridy=3;
    		 JButton first=new JButton("First");
    		 JPanel pannel3 = new JPanel();
    		 pannel3.add(first);
    		 panneau.add(pannel3);
    		 g.setConstraints(pannel3,c);
    		 //******************************************//
     
     
    		 //******************************************//		
    		 c.fill=GridBagConstraints.NONE;
    		 c.gridheight=1;
    		 c.gridwidth=GridBagConstraints.REMAINDER;
    		 c.gridheight=1; 
    		 c.gridx=1;
    		 c.gridy=4;
    		 JButton second=new JButton("second");
    		 JPanel pannel4 = new JPanel();
    		 pannel4.add(second);
    		 panneau.add(pannel4);
    		 g.setConstraints(pannel4,c);
    		 //******************************************//
     
    		return panneau;
    	}
    Ici il manque juste ma dernière ligne mais dès que le retse sera fait, je n'aurai plus de problème!

  5. #5
    Membre averti Avatar de biozaxx
    Profil pro
    Inscrit en
    Août 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 403
    Points : 375
    Points
    375
    Par défaut
    j'ai fais quelques modif (j'ai laissé commanté et il y en a d'autres ou j'ai remplacé les variables (notemment des gridheight par des gridwidth)

    je pense que cela correspond plus a ce que tu veux.
    A toi t'ajuster les poids en fonction du comportement que tu veux lors de la redimension de la 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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
     
     
    private JPanel createPanneau(){        
           //declarations 
           JPanel panneau = new JPanel(); 
           panneau.setBorder(new LineBorder(Color.black)); 
     
           //installer le gestionnaire 
            GridBagLayout g=new GridBagLayout(); 
            panneau.setLayout(g); 
            //créer un objet de type GridBagConstraints 
            GridBagConstraints c=new GridBagConstraints(); 
     
            //******************************************// 
            c.fill=GridBagConstraints.NONE; 
           // c.weightx=1.0; 
            c.gridheight=1; 
            c.gridwidth=4;//GridBagConstraints.REMAINDER; 
            c.gridx=0; 
            c.gridy=0; 
            c.anchor = GridBagConstraints.CENTER; 
            JLabel monLabel1 = new JLabel("Bienvenue"); 
            monLabel1.setFont(new Font("Tahoma", Font.PLAIN, 25)); 
            panneau.add(monLabel1); 
            g.setConstraints(monLabel1,c); 
            //******************************************// 
     
     
            //******************************************//       
            //réinitialisation 
           // c.weightx=0.0; 
           // c.weighty=1.0; 
            c.gridwidth=1; 
            c.gridheight=2; //sur 2 lignes 
            c.gridx=0; 
            c.gridy=1; 
            JLabel monLabel2 = new JLabel("Veuillez choisir les concepts que vous souhaitez mettre en évidence:"); 
            panneau.add(monLabel2); 
            g.setConstraints(monLabel2,c); 
            //******************************************//       
     
            //******************************************//       
            //nouvelle réinitialisation 
          //  c.fill=GridBagConstraints.NONE; 
            //c.weighty=0.0; 
            //c.gridwidth=GridBagConstraints.RELATIVE; 
            c.gridx=1; 
            c.gridy=1; 
            c.gridwidth=1; 
            c.gridheight=2; 
            String[] columnNames = {"Concepts"}; 
            Object[][] data = { 
                     {"Concept1"}, 
                     {"Concept2"}, 
                     {"Concept3"}}; 
            JTable maTable1 = new JTable(); 
            maTable1.setShowGrid(false); 
            JScrollPane scrollPane1 = new JScrollPane(maTable1); 
            JPanel pannel1 = new JPanel(); 
            pannel1.add(scrollPane1); 
            panneau.add(pannel1); 
            g.setConstraints(pannel1,c); 
            //******************************************// 
     
            //******************************************//       
         //   c.fill=GridBagConstraints.NONE; 
            c.gridheight=1; 
            c.gridwidth=1; 
            c.gridheight=1; 
            c.gridx=2; 
            c.gridy=1; 
            c.anchor=GridBagConstraints.SOUTH; 
            JButton transfer1=new JButton(">>"); 
            panneau.add(transfer1); 
            g.setConstraints(transfer1,c); 
            //******************************************//       
     
            //******************************************//       
            //nouvelle réinitialisation 
        //    c.fill=GridBagConstraints.NONE; 
        //    c.gridwidth=GridBagConstraints.REMAINDER; 
            c.gridheight=2; //sur 2 lignes 
            c.gridwidth=1; 
            c.gridx=3; 
            c.gridy=1; 
            String[] columnNames2 = {"Concepts"}; 
            Object[][] data2 = {}; 
            JTable maTable2 = new JTable(); 
            maTable2.setShowGrid(false); 
            JScrollPane scrollPane2 = new JScrollPane(maTable2); 
            JPanel pannel2 = new JPanel(); 
            pannel2.add(scrollPane2); 
            panneau.add(pannel2); 
            g.setConstraints(pannel2,c); 
            //******************************************// 
     
            //******************************************//       
          //  c.fill=GridBagConstraints.NONE; 
            c.gridwidth=1; 
            c.gridheight=1; 
            c.gridx=2; 
            c.gridy=2; 
            c.anchor=GridBagConstraints.NORTH; 
            JButton transfer2=new JButton("<<"); 
            panneau.add(transfer2); 
            g.setConstraints(transfer2,c); 
            //******************************************//    
     
            //******************************************//       
            //réinitialisation 
        //    c.weightx=0.0; 
        //    c.weighty=1.0; 
            c.gridwidth=1; 
            c.gridheight=2; //sur 2 lignes 
            c.gridx=0; 
            c.gridy=3; 
            JLabel monLabel3 = new JLabel("Veuillez choisir la période souhaitée:"); 
            panneau.add(monLabel3); 
            g.setConstraints(monLabel3,c); 
            //******************************************//    
     
     
            //******************************************//       
         //   c.fill=GridBagConstraints.NONE; 
            c.gridwidth=3; 
        //    c.gridwidth=GridBagConstraints.REMAINDER; 
            c.gridheight=1; //sur 2 lignes 
            c.gridx=1; 
            c.gridy=3; 
            JButton first=new JButton("First"); 
            JPanel pannel3 = new JPanel(); 
            pannel3.add(first); 
            panneau.add(pannel3); 
            g.setConstraints(pannel3,c); 
            //******************************************// 
     
     
            //******************************************//       
        //    c.fill=GridBagConstraints.NONE; 
            c.gridwidth=3; 
        //    c.gridwidth=GridBagConstraints.REMAINDER; 
            c.gridheight=1; 
            c.gridx=1; 
            c.gridy=4; 
            JButton second=new JButton("second"); 
            JPanel pannel4 = new JPanel(); 
            pannel4.add(second); 
            panneau.add(pannel4); 
            g.setConstraints(pannel4,c); 
            //******************************************// 
     
           return panneau; 
    }

Discussions similaires

  1. positionnement des composants sur le JFrame
    Par rochdi123 dans le forum Composants
    Réponses: 2
    Dernier message: 09/11/2009, 10h47
  2. [GridBagLayout] Placer des composants sur une grille virtuelle
    Par dev197 dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 13/08/2009, 14h21
  3. positionnement des composants dans une fenetre
    Par thierry_b dans le forum Agents de placement/Fenêtres
    Réponses: 7
    Dernier message: 07/07/2009, 12h27
  4. Réponses: 17
    Dernier message: 17/03/2006, 16h15
  5. [swing][debutant] alignement des composants
    Par melvar dans le forum AWT/Swing
    Réponses: 9
    Dernier message: 10/03/2006, 13h21

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