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

Agents de placement/Fenêtres Java Discussion :

Une série de JButton superposée et centrée sur l'axe Y d'un BorderLayout.EAST?


Sujet :

Agents de placement/Fenêtres Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Une série de JButton superposée et centrée sur l'axe Y d'un BorderLayout.EAST?
    Bonjour à tous les as de la programmation,

    Voici une question probablement trop facile.

    Mais comme je suis débutant, je n'arrive pas à résoudre mon problème.

    Soyez indulgent SVP.

    Je travaille dans un JPanel qui utilise un BorderLayout. Dans ce dernier, je souhaite intégrer dans la section Est "BorderLayout.EAST", quatre JButton qui seront superposés et le tout centré sur l'axe des y.

    // *******EXAMPLE*******
    JPanel panel = new JPanel;
    panel.setLayout(new BorderLayout());

    JPanel buttomPanel = new JPanel;
    buttomPanel.setLayout(new GridLayout(0,1));

    JButton b1 = new JButton("B1");
    JButton b2 = new JButton("B2");
    JButton b3 = new JButton("B3");
    JButton b4 = new JButton("B4");

    buttomPanel.add(b1);
    buttomPanel.add(b2);
    buttomPanel.add(b3);
    buttomPanel.add(b4);

    panel.add(buttomPanel,BorderLayout.EAST);
    // ********* Fin de l'example ********

    Jusque la tout va bien!

    Comment puis-je faire pour les boutons ne soient pas étirés dans toutes l'aire réservée au BorderLayout.EAST....

    Comment dois-je m'y prendre afin d'atteindre mon objectif, merci beaucoup.

    Sébastien, un vrai débutant...

  2. #2
    Membre averti
    Homme Profil pro
    Inscrit en
    Juillet 2002
    Messages
    705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2002
    Messages : 705
    Points : 393
    Points
    393
    Par défaut
    utilse un box layout, ou une gridbag layout.

    bonne lecture.

  3. #3
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 253
    Points
    1 253
    Par défaut
    Premièrement, peux-tu STP utiliser le tag [ CODE ] (sans les espaces entre les crochets et CODE) à la place de tes commentaires ***EXAMPLES*** --> Le dernier bouton (qui est un #) sur la ligne de mise en forme de ton texte met automatiquement ce tag.

    Deuxièmement, utilise le FlowLayout, avec un alignement CENTER dans ton GridLayout.
    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
     JPanel panel = new JPanel;
    panel.setLayout(new BorderLayout());
     
    JPanel buttomPanel = new JPanel;
    buttomPanel.setLayout(new GridLayout(0,1));
     
    JPanel panelButton1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JButton b1 = new JButton("B1");
    panelButton1.add(b1);
    //...
     
    buttomPanel.add(panelButton1);
    //...
     
    panel.add(buttomPanel,BorderLayout.EAST);
    Et voilà !
    Si , et la ont échoué mais pas nous, pensez à dire et cliquez sur . Merci !

    Ici, c'est un forum, pas une foire. Il y a de respectables règles... à respecter !

  4. #4
    Membre du Club Avatar de matzeus
    Inscrit en
    Avril 2006
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 52
    Points : 41
    Points
    41
    Par défaut
    si tu veux, pour une raison qui n'appartient qu'à toi , utiliser a tout prix un gridlayout comme tu as fais, a ce moment là change les arguments de ce gridlayout
    gridlayout(nbrDeLigne,nbrDeColonne)
    dans ton cas c'est :gridlayout(4,1);

    si tu veux, tu peux utiliser 2 autres arguments qui correspondent a l'espacement entre les composants en x et y.

    utilise l'API java, qui t'explique tout !
    http://java.sun.com/j2se/1.3/docs/api/

  5. #5
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 253
    Points
    1 253
    Par défaut
    Citation Envoyé par matzeus
    dans ton cas c'est :gridlayout(4,1);
    +1

    Désolé... je n'y ai même pas fait attention !

    Cela dit à ma décharge, ce que j'ai dit marche aussi...

    [EDIT] Pourquoi faire simple quand on peut faire compliqué ?
    Si , et la ont échoué mais pas nous, pensez à dire et cliquez sur . Merci !

    Ici, c'est un forum, pas une foire. Il y a de respectables règles... à respecter !

  6. #6
    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
    LOL
    si un des deux parametre est pas etre 0 alors c'est bon parce que de toute facon l'autre est calculer. (en fonction du nombre de composant ajouter sur le Container et priorite est donner sur le parametre row).

    par exemple
    si on fait

    panel.setLayout(new GridLayout(3, 0));
    ajouter 10 composant dans le panel.

    il y aura 3 rows et 4 columns.

    si je fais un
    panel.setLayout(new GridLayout(0, 3));
    ajouter 10 composant dans le panel.

    il y aura 4 rows et 3 columns.

    :-))

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Merci à tous pour vos réponses, cependant je crois m'être mal exprimé...

    Voici un exemple complet de code.

    Voici ce que je souhaite obtenir:

    1-Des boutons qui ont la forme de b7 et b8.

    2-Des boutons sont centré sur l'axes de Y. C'est à dire que je souhaite qu'ils soient au centre de la section EAST, et non au nord de la section EAST.

    3-Et si possible, j'aimerais lorsque l'utilisateur modifie manuellement la taille du JFrame, que mes bouttons se comportent comme b3 et b4; qu'ils ne disparaissent pas de l'écran quoi!

    Je vous remercie grandement de votre aide.

    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
     
    import javax.swing.*;
    import java.awt.*;
     
    public class Toto extends JFrame {
    	protected JButton b1,b2,b3,b4,b5,b6,b7,b8;
     
    	public Toto() {
    		setTitle("toto");
    		this.getContentPane().setLayout(null);
     
    		getContentPane().setLayout(new BorderLayout());
    		JPanel northButtonBar = new JPanel(new FlowLayout(FlowLayout.CENTER));
    		JPanel westButtonBar = new JPanel(new GridLayout(2,1));
    		JPanel centerButtonBar = new JPanel();
    		JPanel eastButtonBar = new JPanel();
    		eastButtonBar.setLayout(new BoxLayout(eastButtonBar, BoxLayout.Y_AXIS));
    		JPanel southProgress = new JPanel (new FlowLayout(FlowLayout.CENTER));
     
    		JProgressBar pb = new JProgressBar();
    		b1 = new JButton("1");
    		b2 = new JButton("2");
    		b3 = new JButton("3");
    		b4 = new JButton("4");
    		b5 = new JButton("5");
    		b6 = new JButton("6");
    		b7 = new JButton("7");
    		b8 = new JButton("8");
     
    		add(addButtons(northButtonBar,b1,b2),BorderLayout.NORTH);
    		add(addButtons(westButtonBar,b3,b4),BorderLayout.WEST);
    		add(addButtons(centerButtonBar,b5,b6),BorderLayout.CENTER);
    		add(addButtons(eastButtonBar,b7,b8),BorderLayout.EAST);
    		add(southProgress.add(pb), BorderLayout.SOUTH);
    		this.setSize(600,600);
    		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		SymWindow aSymWindow = new SymWindow();
    	    this.addWindowListener(aSymWindow);
    	}	
     
    	public JPanel addButtons(JPanel pane, JButton xb1, JButton xb2) {
    		pane.add(xb1);
    		pane.add(xb2);
    		return(pane);
    	}
     
     
    	private class SymWindow extends java.awt.event.WindowAdapter {
    	    public void windowClosing(java.awt.event.WindowEvent event) {
    		    setVisible(false);
    		    System.exit(0);
    	    }
    	}
     
     
    	private static void createAndShowGUI() {
     		Toto frame = new Toto();
            frame.setVisible(true);
        }
     
    	public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    	}	
    }
    Merci encore

  8. #8
    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
    test ca parce que j'"ai pas tout comprit ce que tu veut.

    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
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
     
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     */
    public class Toto2 extends JPanel {
        private JPanel topPanel = new JPanel();
        private JPanel eastPanel = new JPanel();
        private JPanel westPanel = new JPanel();
        private JPanel centerPanel = new JPanel();
        private JPanel southPanel = new JPanel();
     
        private JButton one = new JButton();
        private JButton two = new JButton();
        private JButton three = new JButton();
        private JButton four = new JButton();
        private JButton five = new JButton();
        private JButton six = new JButton();
        private JButton seven = new JButton();
        private JButton eight = new JButton();
     
        private JButton ten = new JButton();
     
        public Toto2() {
            initGUI();
        }
     
        private void initGUI() {
            setLayout(new BorderLayout());
     
            topPanel.setLayout(new GridBagLayout());
            eastPanel.setLayout(new GridBagLayout());
            westPanel.setLayout(new GridBagLayout());
            centerPanel.setLayout(new GridBagLayout());
            southPanel.setLayout(new GridBagLayout());
     
            one.setText("1");
            two.setText("2");
            three.setText("3");
            four.setText("4");
            five.setText("5");
            six.setText("6");
            seven.setText("7");
            eight.setText("8");
            ten.setText("10"); // no nine :-)
     
            // north
            topPanel.add(one,
                    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
                            new Insets(5, 0, 5, 0), 0, 0));
            topPanel.add(two,
                    new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            add(topPanel, BorderLayout.NORTH); // or BorderLayout.PAGE_START
     
            // west
            westPanel.add(three,
                    new GridBagConstraints(0, 0, 1, 1, 1.0, 0.5, GridBagConstraints.CENTER,
                            GridBagConstraints.BOTH,
                            new Insets(0, 0, 0, 0), 0, 0));
            westPanel.add(four,
                    new GridBagConstraints(0, 1, 1, 1, 0.0, 0.5, GridBagConstraints.CENTER,
                            GridBagConstraints.BOTH,
                            new Insets(0, 0, 0, 0), 0, 0));
            add(westPanel, BorderLayout.WEST);
     
            // east
            eastPanel.add(eight,
                    new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                            GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            eastPanel.add(seven,
                    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                            GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            add(eastPanel, BorderLayout.EAST);
     
            // center
            centerPanel.add(five, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
                    new Insets(0, 0, 0, 0), 0, 0));
            centerPanel.add(six, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
                    new Insets(0, 0, 0, 0), 0, 0));
            add(centerPanel, BorderLayout.CENTER);
     
            //south
            southPanel.add(ten,
                    new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                            new Insets(5, 0, 5, 0), 0, 0));
            add(southPanel, BorderLayout.SOUTH);
     
        }
     
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JFrame myFrame = new JFrame("My frame");
                    myFrame.add(new Toto2());
                    myFrame.setSize(400, 300);  // pack();
                    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myFrame.setLocationRelativeTo(null);
                    myFrame.setVisible(true);
                }
            });
        }
    }


    ps: le code est pas terrible mais c'est du code generer et du copy/paste. tout est fait en moins de 3 minutes

  9. #9
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Bonjour à tous,

    J'ai trouvé la solution grâce à vos nombreux commentaire.

    J'ai combiner le gridlayout a un gridbaglayout et voilà le tour est joué...

    Merci beaucoup

    Sébastien

  10. #10
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 253
    Points
    1 253
    Par défaut
    Citation Envoyé par sdurand
    J'ai trouvé la solution grâce à vos nombreux commentaire.
    Est-ce que tu pourrais la poster STP ?
    Si , et la ont échoué mais pas nous, pensez à dire et cliquez sur . Merci !

    Ici, c'est un forum, pas une foire. Il y a de respectables règles... à respecter !

  11. #11
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Voici donc ce que je voulais obtenir...

    C'est une colonne de Jbotton centré sur l'axes des y.
    Le boutons ne sont pas étirés.
    Et lorsque la fenêtre de rapetissée, les boutons fond de même...

    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
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
     
    public class Toto extends JPanel {
        private JPanel eastPanel = new JPanel();
     
        private JButton one = new JButton();
        private JButton two = new JButton();
        private JButton three = new JButton();
        private JButton four = new JButton();
     
        public Toto() {
            initGUI();
        }
     
        private void initGUI() {
            setLayout(new BorderLayout());
            JPanel ePanel = new JPanel();
     
            ePanel.setLayout(new GridLayout(4,1));
     
            eastPanel.setLayout(new GridBagLayout());
     
            one.setText("1");
            two.setText("2");
            three.setText("3");
            four.setText("4");
     
            add(eastPanel, BorderLayout.EAST);
     
            ePanel.add(one);
            ePanel.add(two);
            ePanel.add(three);
            ePanel.add(four);
     
            eastPanel.add(ePanel,
                    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                            GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            add(eastPanel, BorderLayout.EAST);
     
        }
     
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JFrame myFrame = new JFrame("My frame");
                    myFrame.add(new Toto());
                    myFrame.setSize(400, 300);  // pack();
                    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myFrame.setLocationRelativeTo(null);
                    myFrame.setVisible(true);
                }
            });
        }
    }
    Sébastien

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 27/09/2007, 15h31
  2. Superposer Anim Flash sur une image JPG
    Par parilla77 dans le forum Intégration
    Réponses: 2
    Dernier message: 19/06/2007, 12h09
  3. Réponses: 7
    Dernier message: 11/06/2007, 14h11
  4. Cadre centré sur une page
    Par mrkinfo dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 21/02/2007, 21h25
  5. [HTML] HTML: Superposer du texte sur une image
    Par claralavraie dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 09/02/2006, 13h44

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