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 :

paintComponent dans JPanel


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de Willy55
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    188
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 188
    Par défaut paintComponent dans JPanel
    Bonjour,

    Je voudrais faire apparaitre un rectangle dans un jpanel existant lorsque l'on clique sur un bouton, en utilisant paintComponent.

    Mon rectangle qui doit apparaitre :

    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
    package metier;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    public class Rectangle extends JPanel {
     
    	@Override
        public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.RED);
    		g.fillRect(0,0,50,50);
    	}
    }
    Lorsque que je l'ajoute au jpanel :

    Rectangle ab = new Rectangle ();
    jPanel_grille.add(ab);

    je ne le vois pas...

  2. #2
    Modérateur
    Avatar de wax78
    Homme Profil pro
    R&D - Palefrenier programmeur
    Inscrit en
    Août 2006
    Messages
    4 096
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : R&D - Palefrenier programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 096
    Par défaut
    Peut être parce qu'il n'as pas de taille ?
    Que ton panel général utilise un layout spécial?
    Difficile a dire sans avoir le code qui englobe le panel incriminé.
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre confirmé Avatar de Willy55
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    188
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 188
    Par défaut
    Je fais mon interface graphique avec le GUI Netbeans, j'ai juste mis un jpanel dans la jframe, un bouton "lancer" sur lequel j'ai mis dans la méthode actionPerformed du bouton :

    Rectangle ab = new Rectangle ();
    jPanel_grille.add(ab);

    mais sa ne donne rien...

    je le code de la vue, générer en grande partie par Netbeans

    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
    package vue;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import metier.Abeille;
    import metier.Dot;
     
    public class Grille extends javax.swing.JFrame {
     
        public Grille() {
            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.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jPanel_grille = new javax.swing.JPanel();
            jPanel_panneau = new javax.swing.JPanel();
            jButton_lancer = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            javax.swing.GroupLayout jPanel_grilleLayout = new javax.swing.GroupLayout(jPanel_grille);
            jPanel_grille.setLayout(jPanel_grilleLayout);
            jPanel_grilleLayout.setHorizontalGroup(
                jPanel_grilleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );
            jPanel_grilleLayout.setVerticalGroup(
                jPanel_grilleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 490, Short.MAX_VALUE)
            );
     
            jButton_lancer.setText("Lancer");
            jButton_lancer.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton_lancerActionPerformed(evt);
                }
            });
     
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
     
            jLabel1.setText("Nb d'abeille :");
     
            javax.swing.GroupLayout jPanel_panneauLayout = new javax.swing.GroupLayout(jPanel_panneau);
            jPanel_panneau.setLayout(jPanel_panneauLayout);
            jPanel_panneauLayout.setHorizontalGroup(
                jPanel_panneauLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_panneauLayout.createSequentialGroup()
                    .addGap(306, 306, 306)
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 397, Short.MAX_VALUE)
                    .addComponent(jButton_lancer)
                    .addGap(58, 58, 58))
            );
            jPanel_panneauLayout.setVerticalGroup(
                jPanel_panneauLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel_panneauLayout.createSequentialGroup()
                    .addGap(62, 62, 62)
                    .addGroup(jPanel_panneauLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton_lancer)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addContainerGap(62, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel_grille, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel_panneau, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jPanel_panneau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jPanel_grille, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
     
        }                                           
     
        private void jButton_lancerActionPerformed(java.awt.event.ActionEvent evt) {                                               
    		Rectangle ab = new Rectangle ();
    		jPanel_grille.add(ab);
    		jPanel_grille.updateUI();
        }                                              
     
     
    //    public static void main(String args[]) {
    //        java.awt.EventQueue.invokeLater(new Runnable() {
    //            public void run() {
    //                new Grille().setVisible(true);
    //            }
    //        });
    //    }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton_lancer;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel_grille;
        private javax.swing.JPanel jPanel_panneau;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    tu utiliser un grouplayout -> Il faut établir des contrainte pour ton nouveau Rectangle dans ce layout (t'as pas pris le plus facile des layouts)
    ton REctangle n'a pas de taille minimum, comme il ne contient rien, il risque d'être mis à la taille 0,0
    enfin, tu dois appeler sur le parent la méthode revalidate() après l'ajout.

  5. #5
    Membre confirmé Avatar de Willy55
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    188
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 188
    Par défaut
    Merci de votre aide

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

Discussions similaires

  1. Problème avec image dans JPanel
    Par littleshrimp dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 14/05/2006, 14h05
  2. Espace entre élément dans JPanel
    Par Kiboumz dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 12/04/2006, 11h27
  3. [SWING] JFrame dans JPanel = possible?
    Par rprom1 dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 22/03/2006, 16h03
  4. Problème avec l'insertion d'une image dans JPanel
    Par anutka dans le forum AWT/Swing
    Réponses: 1
    Dernier message: 16/03/2006, 10h02
  5. Positionement dans JPanel
    Par arsenik7 dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 02/02/2006, 15h29

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