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 :

nombre de JPanel variable dans une JFrame


Sujet :

Agents de placement/Fenêtres Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut nombre de JPanel variable dans une JFrame
    bonjour,

    Je suis assez novice en Java, et j'essaye de créer ceci avec Net Beans:

    j'ai vréé une JFrame comportant un libellé et
    j'ai un Jframe (main) dans lequel je voudrait afficher à des emplacement précis ce Jfrme (donc, l'afficher par exemple 4 fois).

    comment puis-je m'y prendre ???

    Merci

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    13
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2008
    Messages : 13
    Par défaut
    Sorry mais j'capte pas ta question, ou plutot l'expression de ton besoin...

    Peux-tu réexpliquer ça?

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut
    OK,

    par exemple, supposons que sur une fenêtre principale (JFrame) j'ai un Edit et un bouton. En fonction d'une valeur entrée dans l'Edit (1 à 10) j'afficher une même fenêtre (ici mon JPanel) (1 à 10 fois) dans la Jframe (qui contient des edit, des boutons, ...) à des place X,Y.

    j'espère que ceci rendra la question plus facile à comprendre...

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    13
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2008
    Messages : 13
    Par défaut
    Genre dans la fenêtre suivante, tu cliques sur le bouton "OK" et tu as 2 fois (nombre du JTextField) le même JPanel (contenant le JLabel, le JTextField et le JButton) qui s'affiche?

  5. #5
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut
    C'est bien ça, mais le JPanel s'affiche dans la fenêtre courrante (où tu as ton bouton qui affiche les X JPanel)

    voici un exemple

    Nom : Sans titre.JPG
Affichages : 82
Taille : 15,8 Ko

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    13
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2008
    Messages : 13
    Par défaut
    OK, ça devient plus clair!
    Hé bien tout dépend de comment tu veux agencer tes JPanel... Cela va déterminer le Layout qui va contenir tous tes JPanel.
    Dans ta JFrame, mets de côté tout ce qui ne doit pas bouger et tu ajoutes en dessous un JPanel (monPanelConteneur, penser à faire un setLayout() dessus), qui va contenir tous tes JPanels ajoutés.
    Fais une classe à part pour le type de JPanel ajouté (MonJPanel).
    Quand tu appuieras sur ton bouton pour ajouter, il faudra faire une boucle du type
    for i=1 to x
    monPanelConteneur.add(new MonJPanel()).
    Mais comme je te l'ai déjà dit, le Layout gèrera tout ton agencement des panels, donc pense à faire le setLayout() avant.

  7. #7
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut
    C'est bien ce que je fais, mais rien ne s'affiche dans le JPanel ???

    si ca peut aider, voici le code de la frame
    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
     
    package projetsdlbis;
     
    import org.jdesktop.application.Action;
     
    public class NewJFrame extends javax.swing.JFrame {
     
        public NewJFrame() {
            initComponents();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jTextField1 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setName("Form"); // NOI18N
     
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(projetsdlbis.ProjetSDLbisApp.class).getContext().getResourceMap(NewJFrame.class);
            jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
            jTextField1.setName("jTextField1"); // NOI18N
     
            javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(projetsdlbis.ProjetSDLbisApp.class).getContext().getActionMap(NewJFrame.class, this);
            jButton1.setAction(actionMap.get("replique")); // NOI18N
            jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
            jButton1.setName("jButton1"); // NOI18N
     
            jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            jPanel1.setName("jPanel1"); // NOI18N
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 322, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 206, Short.MAX_VALUE)
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(112, 112, 112)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(58, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(31, 31, 31)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
     
     
        @Action
        public void replique() {
     
            JPanel_Multiple jpm = new JPanel_Multiple();
            jPanel1.add(new JPanel_Multiple());
        }
     
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
     
    }

    et le code du JPanel
    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
     
    package projetsdlbis;
     
    public class JPanel_Multiple extends javax.swing.JPanel {
     
        /** Creates new form JPanel_Multiple */
        public JPanel_Multiple() {
            initComponents();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
     
            setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            setName("Form"); // NOI18N
     
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(projetsdlbis.ProjetSDLbisApp.class).getContext().getResourceMap(JPanel_Multiple.class);
            jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
            jLabel1.setName("jLabel1"); // NOI18N
     
            jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
            jTextField1.setName("jTextField1"); // NOI18N
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(18, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(58, Short.MAX_VALUE))
            );
        }// </editor-fold>
     
     
        // Variables declaration - do not modify
        private javax.swing.JLabel jLabel1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
     
    }

  8. #8
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    13
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2008
    Messages : 13
    Par défaut
    En survolant (rapidement) ton code, je n'ai pas trouvé d'appel de la méthode validate().
    Si tu ne l'as pas fait, appelle la après tes ajouts de panels.

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

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Plutôt revalidate qui a le même effet dans le cas présent mais qui est moins lourde niveau perfs que validate.

  10. #10
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    13
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2008
    Messages : 13
    Par défaut
    Merci pour l'info, je note!

  11. #11
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut
    j'avais déjà essayé le validate après le add, mais rien n'y fait, mon Panel reste désespérément vide

  12. #12
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    13
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2008
    Messages : 13
    Par défaut
    Dans ce cas, je ne sais plus quoi te conseiller!

  13. #13
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut
    J'ai trouvé,

    j'ai ajouté un setLayout avant le add et ca fonctionne

    Merci pour votre aide

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

Discussions similaires

  1. Problème décalage du JPanel dans une JFrame a cause du JMenuBar
    Par matou72 dans le forum Agents de placement/Fenêtres
    Réponses: 5
    Dernier message: 09/03/2007, 21h17
  2. ajouter une jpanel dans le bon emplacement dans une jframe
    Par jlassiramzy dans le forum Agents de placement/Fenêtres
    Réponses: 1
    Dernier message: 31/07/2006, 18h37
  3. Positionner précisement plusieurs JPanel dans une JFrame.
    Par RadicalBob dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 13/06/2006, 23h42
  4. Gestion de menu, JPanels dans une JFrame
    Par Doc.Fusion dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 03/12/2004, 19h27

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