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 :

Layout vertical avec centrage


Sujet :

AWT/Swing Java

  1. #1
    Membre actif Avatar de trax44
    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 300
    Points : 233
    Points
    233
    Par défaut Layout vertical avec centrage
    Bonjour,

    je cherche un layout qui me permettrait de faire l'équivalent d'un FlowLayout mais en vertical.

    un peu comme l'image ci dessus mais en faisant en sorte que tous les bouttons aient la largeur du plus grand.

    Cordialement

    trax

  2. #2
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    BoxLayout vertical ou GridLayout de 1 colonne
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  3. #3
    Membre actif Avatar de trax44
    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 300
    Points : 233
    Points
    233
    Par défaut
    Le GridLayout prend toute la largeur possible
    BoxLayout les cases ont pas la meme largeurs (du moins je n'y arrive pas)

    J'ai trouvé une combinaisons de GridLayout et de FlowLayout :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    fen = new JFrame ("salut :)");
    plip = new JButton("plip");
    plup = new JButton("plup");
    nos = new JPanel (new FlowLayout());
    pan = new JPanel (new GridLayout(2,1));
     
    pan.add (plip);
    pan.add (plup);
    nos.add(pan);
    fen.add(nos);
    Mais je trouve pas ça top :s

  4. #4
    Membre éclairé
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Points : 704
    Points
    704
    Par défaut
    Utilise un GridBagLayout

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0; // gridy est par defaut sur RELATIVE.
    gbc.fill = GridBagConstraints.HORIZONTAL;
     
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    gbl.setConstraints(gbc);
     
    container.add(button1);
    container.add(button2);
    container.add(button3);
    ...
    si j'ai rien oublier et j'ai pas fait de faute d'orthographe ca doit aller ca

  5. #5
    Membre éclairé
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Points : 704
    Points
    704
    Par défaut
    je viens de faire un exemple

    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
     
    package faqs.q03;
     
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Dimension;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JButton;
     
    /**
     * removed
     * @author bebe (bbclone@gmail.com)
     */
    public class Q03b {
     
        private JFrame mainFrame = null;
     
        public Q03b() {
            mainFrame = new JFrame("...");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            /* 1- Initialisation du container. */
            mainFrame.setLayout(new GridBagLayout());
     
            /* 2- Création et initialisation d'une série de composants. */
            String sentence = "Le GridBagLayout est un gestionnaire de placement";
            String[] words = sentence.split(" ");
            JButton[] buttons = new JButton[words.length];
            for (int i = 0; i < words.length; i++) {
                System.out.println(words[i]);
                buttons[i] = new JButton(words[i]);
            }
     
            /*3- Ajout de ces composants en spécifiant les contraintes de type GridBagConstraints. */
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = GridBagConstraints.RELATIVE;
            gbc.fill = GridBagConstraints.HORIZONTAL; 
     
            for (JButton button : buttons) {
                mainFrame.add(button, gbc);
            }
     
            mainFrame.setMinimumSize(new Dimension(400, 300));
            mainFrame.setLocationRelativeTo(null);
        }
     
         public void setVisible(boolean b) {
            mainFrame.setVisible(b);
        }
     
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    Q03b q03b = new Q03b();
                    q03b.setVisible(true);
                }
            });
        }
     
    }

Discussions similaires

  1. [CSS 3] Centrage vertical avec bootstrap
    Par Jeannot40 dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 10/04/2015, 10h07
  2. [Forum] Menu vertical avec ouverture automatique
    Par gregius dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 9
    Dernier message: 22/08/2006, 18h59
  3. Réponses: 13
    Dernier message: 16/08/2006, 09h06
  4. Accéder à <layout:text> avec Javascript
    Par micanti dans le forum Struts 1
    Réponses: 2
    Dernier message: 30/05/2006, 17h59
  5. [Struts-layout]err. avec layout:menuItem
    Par javazer dans le forum Struts 1
    Réponses: 7
    Dernier message: 14/04/2006, 14h08

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