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 :

[Swing] Utilisation des BoxLayout


Sujet :

Agents de placement/Fenêtres Java

  1. #1
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Points : 100
    Points
    100
    Par défaut [Swing] Utilisation des BoxLayout
    Bonjour,

    J'ai écrit une classe qui est affiche une boite de dialogue qui contient:
    - un label sur la premiere ligne
    - un label et une combo box (l'un à côté de l'autre) sur la 2e ligne
    - un label, une combo box et jtexte field, l'un à coté de l'autre, sur la 3e ligne
    - un bouton ok et un bouton cancel à la 4 ligne

    mais g un pb de dimensionnement et le résultat n'est pas très beau:

    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
     
    import javax.swing.* ;
    import java.awt.*;
     
    public class Dialog extends JDialog{
     
     
     private  JPanel pnlRecoFV ;
     private  JLabel recoFVInstrumentLbl ;
     private  JLabel recommendationLbl ;
     private  JComboBox recommendationBox ; 
     private  JLabel fairValueLbl ;
     private  JComboBox fairValueBox ;
     private  JTextField fairValueText ; 
     private  JPanel pnlInstrument ;
     private  JPanel pnlReco ;
     private  JPanel pnlFV ;
     private  JButton addButton ;
     private  JButton cancelButton ;
     private  JPanel pnlButtonsRecoFV ; 
     
     
    //********** Constructeur ****************
     
    public Dialog() {
     
      super();
     
      pnlRecoFV = new JPanel() ;
      recoFVInstrumentLbl = new JLabel() ;
      recommendationLbl = new JLabel() ;
      recommendationBox = new JComboBox() ;
      fairValueLbl = new JLabel() ;
      fairValueBox = new JComboBox() ;
      fairValueText = new JTextField() ;
      pnlInstrument = new JPanel() ;
      pnlReco = new JPanel() ;
      pnlFV = new JPanel() ;
      addButton = new JButton("Add") ;
      cancelButton = new JButton("Cancel") ;
      pnlButtonsRecoFV = new JPanel() ;
     
     
        pnlRecoFV.setLayout(new BoxLayout(pnlRecoFV, BoxLayout.Y_AXIS));
        pnlInstrument.setLayout(new BoxLayout(pnlInstrument, BoxLayout.X_AXIS));
        pnlReco.setLayout(new BoxLayout(pnlReco, BoxLayout.X_AXIS));
        pnlFV.setLayout(new BoxLayout(pnlFV, BoxLayout.X_AXIS));
        pnlButtonsRecoFV.setLayout(new BoxLayout(pnlButtonsRecoFV, BoxLayout.X_AXIS));
     
        recoFVInstrumentLbl.setText("Instrument: ");
        recommendationLbl.setText("Recommendation :");
        fairValueLbl.setText("Fair value: ");
     
        recommendationBox.setPreferredSize(new Dimension(100, 80));
        fairValueBox.setPreferredSize(new Dimension(100, 80));
     
        pnlInstrument.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pnlInstrument.add(recoFVInstrumentLbl);
     
        pnlReco.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pnlReco.add(recommendationLbl);
        pnlReco.add(recommendationBox);
     
        pnlFV.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pnlButtonsRecoFV.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pnlFV.add(fairValueLbl);
        pnlFV.add(fairValueBox);
        pnlFV.add(fairValueText);  
        pnlButtonsRecoFV.add(addButton);
        pnlButtonsRecoFV.add(cancelButton);
     
        pnlRecoFV.add(pnlInstrument);
        pnlRecoFV.add(pnlReco);
        pnlRecoFV.add(pnlFV);
        pnlRecoFV.add(pnlButtonsRecoFV);
     
        this.add(pnlRecoFV);
        this.setVisible(true); 
     
        pack() ;
     
     }
     
    //********************* main *********************
     
    public static void main(String[] argv) {
    	Dialog dialog = new Dialog() ;
     
     }
     
    }//class
    La boite de dialogue contient le panel principal "pnlRecoFV" qui contient à son tour 4 panels: pnlInstrument, pnlReco, pnlFV et pnlButtonsRecoFV dans l'ordre, du haut vers le bas.
    Le layout manager que g utilisé est le BoxLayout.
    donc, pnlRecoFV aligne ses panels sur l'axe Y (vertical) et tous les autres panels alignent leurs composants sur l'axe X (horizontal)

    est-ce que quelqu'un peut m'aider à avoir un meilleur résultat svp?

    Merci
    Rien ne sert de courir, il faut partir à point.

  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
    salut,

    premierement tu devrais utiliser:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    this.getContentPane().add(pnlRecoFV); 
    au lieu de this.add(pnlRecoFV);
    ensuite le rendu est normal etant donné que tu as utilisé un BoxLayout !

    si tu veux un rendu plus souple et plus joli, ou tu pourras proportionner tes 'cases' il faut que tu utilises un GridBagLayout. C'est plus compliqué à utiliser mais le rendu est bien meilleur.

    tu peux jeter un oeil ici:
    http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html

  3. #3
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Points : 100
    Points
    100
    Par défaut
    mais regarde ici: http://java.sun.com/j2se/1.5.0/docs/api/index.html comment est-ce qu'ils ont pu obtenir un beau résultat rien qu'en utilisant les boxlayout? regarde la boite de dialogue Name chooser.

    Merci
    Rien ne sert de courir, il faut partir à point.

  4. #4
    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
    mais regarde ici: http://java.sun.com/j2se/1.5.0/docs/api/index.html
    heu l'url correspond à l'index de l'API , je ne vois aucun exemple ici

  5. #5
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Points : 100
    Points
    100
    Rien ne sert de courir, il faut partir à point.

  6. #6
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Points : 100
    Points
    100
    Par défaut
    nos 2 msges se sont croisés !!
    Rien ne sert de courir, il faut partir à point.

  7. #7
    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
    Apparament ils ont utilisés 2 BoxLayout et le tout est dans un BorderLayout qui permet de controler la taille du jscrollpane

  8. #8
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Points : 100
    Points
    100
    Par défaut
    salut,

    J'ai travaillé avec les GridBagLayout et les boxlayout.
    Le GridBagLayout concerne le panel principal et le boxlayout est le layout pour chaque sous panel.
    De plus, j'ai donné des dimensions aux différents composants:

    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
     
    //*************** Les attributs *****************
     
    JPanel pnlRecoFV = new JPanel() ;
      JLabel recoFVInstrumentLbl = new JLabel() ;
      JLabel recommendationLbl = new JLabel() ;
      JComboBox RecommendationBox = new JComboBox() ;
      JLabel FairValueLbl = new JLabel() ;
      JComboBox FairValueBox = new JComboBox() ;
      JTextField FairValueText = new JTextField() ;
     
    /************** Le constructeur *****************
     
        pnlRecoFV.setLayout(new GridBagLayout());
        BoxLayout.X_AXIS));
        m_InstrumentRecoFVInstrumentPanel = new InstrumentFVPanel();
        pnlReco.setLayout(new BoxLayout(pnlReco, BoxLayout.X_AXIS));
        pnlFV.setLayout(new BoxLayout(pnlFV, BoxLayout.X_AXIS));
        pnlButtonsRecoFV.setLayout(new BoxLayout(pnlButtonsRecoFV,     BoxLayout.Y_AXIS));
       JPanel pnlReco = new JPanel() ;
      JPanel pnlFV = new JPanel() ;
      JButton addButton = new JButton("Add") ;
      JButton cancelButton = new JButton("Cancel") ;
      JPanel pnlButtonsRecoFV = new JPanel() ; 
     
        recoFVInstrumentLbl.setText("Instrument: ");
        recommendationLbl.setHorizontalAlignment(SwingConstants.LEFT);
        recommendationLbl.setText("Recommendation :");
        FairValueLbl.setText("Fair value: ");
        FairValueLbl.setHorizontalAlignment(SwingConstants.LEFT);
     
        RecommendationBox.setMaximumSize(new Dimension(150, 22));
        RecommendationBox.setMinimumSize(new Dimension(150, 22));
        RecommendationBox.setPreferredSize(new Dimension(150, 22));
     
         pnlReco.setBorder(
                BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Add a new recommendation"),
                BorderFactory.createEmptyBorder(10,10,10,10)));
     
        pnlReco.setLayout(flowLayout1);
        flowLayout1.setAlignment(FlowLayout.LEFT);
        pnlReco.add(recommendationLbl, null);
        pnlReco.add(RecommendationBox, null);
     
        FairValueBox.setMaximumSize(new Dimension(150, 22));
        FairValueBox.setMinimumSize(new Dimension(150, 22));
        FairValueBox.setPreferredSize(new Dimension(150, 22));
     
        FairValueText.setMaximumSize(new Dimension(150, 22));
        FairValueText.setMinimumSize(new Dimension(150, 22));
        FairValueText.setPreferredSize(new Dimension(150, 22));
     
        pnlFV.setBorder(
                BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Add a new fair value"),
                BorderFactory.createEmptyBorder(10,10,10,10)));
      
        FlowLayout flowLayout1 = new FlowLayout() ;
        pnlFV.setLayout(flowLayout1);
        flowLayout10.setAlignment(FlowLayout.LEFT);
        pnlFV.add(FairValueLbl, null);
        pnlFV.add(FairValueBox, null);
        pnlFV.add(FairValueText, null);
     
        pnlButtonsRecoFV.add(addButton);
        pnlButtonsRecoFV.add(cancelButton);
     
        pnlRecoFV.add(m_InstrumentRecoFVInstrumentPanel, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0
                              ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
     
        pnlRecoFV.add(pnlReco, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0
                             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
     
        pnlRecoFV.add(pnlFV, new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0
                             ,GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
     
        pnlRecoFV.add(pnlButtonsRecoFV, new GridBagConstraints(5, 0, 1, 5, 0.0, 1.0
                             ,GridBagConstraints.NORTH, GridBagConstraints.VERTICAL, new Insets(5, 5, 5, 5), 0, 0));
     
     
        tabCompany.addTab("Recommendation & Fair Value", pnlRecoFV) ;
    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
     
    //************* La classe InstrumentFVPanel ***********
    public class InstrumentFVPanel extends JPanel{
        JLabel lblInstrument = new JLabel();
        JLabel MainInstrument = new JLabel();
          FlowLayout flowLayout1 = new FlowLayout();
     
          public InstrumentFVPanel() {
            try {
              jbInit();
            }
            catch(Exception ex) {
              ex.printStackTrace();
            }
          }
          void jbInit() throws Exception {
            lblInstrument.setHorizontalAlignment(SwingConstants.LEFT);
            lblInstrument.setText("Instrument:");
            this.setLayout(flowLayout1);
            flowLayout1.setAlignment(FlowLayout.LEFT);
            this.add(lblInstrument, null);
            MainInstrument.setText("Main Instrument");  
            this.add(MainInstrument, null);
          }
     
    }
    Rien ne sert de courir, il faut partir à point.

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

Discussions similaires

  1. [GF 4.0] Quels jars nécessaires pour une application Swing utilisant des EJB
    Par Népomucène dans le forum Glassfish et Payara
    Réponses: 1
    Dernier message: 01/09/2014, 09h29
  2. Probleme d'utilisation des sockets avec Swing
    Par tuxman dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 09/11/2008, 11h57
  3. Utiliser des objets SWING dans une vue RCP
    Par manuga72 dans le forum Eclipse Platform
    Réponses: 1
    Dernier message: 20/10/2006, 17h26
  4. [Crystal Report] Utilisation des vues de sql serveur
    Par Olivierakadev dans le forum SAP Crystal Reports
    Réponses: 2
    Dernier message: 15/11/2002, 17h44
  5. [BCB5] Utilisation des Ressources (.res)
    Par Vince78 dans le forum C++Builder
    Réponses: 2
    Dernier message: 04/04/2002, 16h01

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