Creer un jPanel

Steph115 Messages postés 163 Date d'inscription mercredi 22 octobre 2003 Statut Membre Dernière intervention 3 février 2012 - 10 avril 2008 à 11:32
Steph115 Messages postés 163 Date d'inscription mercredi 22 octobre 2003 Statut Membre Dernière intervention 3 février 2012 - 10 avril 2008 à 15:38
Bonjour,

J'utilise ce code ci dessous pour insérer un jpanel qui a une image de fond dans un jpanel existant.
Le jpanel ne s'affiche pas dans l'application, et j'ai mis un sysout dans la redefinission du paintComponent de la classe JImagePanel et il ne s'affiche jamais.
N'y aurait il pas un problème avec les layout ?
Ne faut il pas spécifier des layout dans le nouveau jPanel qu'il créer ?
Je vous joint ci dessous le descriptif des layout de mon panel existant.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String chemin = "/ressources/img/pingouins.jpg";
        URL url = this.getClass().getResource(chemin);
        System.out.println(url.toString());
        jLabel1.setText(url.toString());
       
        // On met l'image sur le jLabel
        jLabel2.setIcon(new javax.swing.ImageIcon(url));
       
        /** Accès au toolkit : */
        java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
        /** lecture de l'image : */
        Image image = toolkit.getImage(url);
       
        //BufferedImage image = ImageIO.read( /* File, URL ou InputStream */ );
        JImagePanel jp = new JImagePanel(image, 10, 0, true, true);
       
        jPanel1.add(jp);
        jPanel1.updateUI();
    }

Et voici le code du layout

private void initComponents() {



        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();



        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Loader Image");



        jLabel1.setText("jLabel1");



        jButton1.setText("Cliquez Ici");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });



        jLabel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Label"));



        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Panel"));



        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 144, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, 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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jButton1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                .addContainerGap())
        );



        pack();
    }



Ci quelqu'un a une idée qu'il n'hésite surtout pas car la je ne sais vraiment plus quoi faire.

La classe JImagePanel est visible sur ce site la : http://www.javafr.com/codes/JIMAGEPANEL-PANEL-AFFICHANT-IMAGES-REDIMENSIONNEES-AVEC-MEME-ASPECT_42125.aspx

Merci. ++ Nexus

Chaque problème a sa solution. S'il n'y a pas de solution c'est qu'il n'y a pas de problème.

1 réponse

Steph115 Messages postés 163 Date d'inscription mercredi 22 octobre 2003 Statut Membre Dernière intervention 3 février 2012
10 avril 2008 à 15:38
Entre temps j'ai trouvé la solution il fallait définir une taille sur le jPanel voici le code final :

// Panel avec Image
JImagePanel jp = new JImagePanel(image, 10, 0, true, true);
jp.setSize(300, 300);
jp.setLocation(100, 100);
jPanel1.add(jp);

Merci. ++ Nexus

Chaque problème a sa solution. S'il n'y a pas de solution c'est qu'il n'y a pas de problème.
0
Rejoignez-nous