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

Composants Java Discussion :

Changer l'image d'un Jlabel


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Inscrit en
    Avril 2012
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2012
    Messages : 1
    Par défaut Changer l'image d'un Jlabel
    Bonjour, j'essaye de changer dynamiquement l'image d'un jlabel quand je clique sur un bouton, mais ça ne marche pas. voici le code de l'application :
    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
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package org.gillesmaury;
     
    /**
     *
     * @author gilles
     */
    public class ImageReactiveAuBouton extends javax.swing.JFrame {
     
        /**
         * Creates new form ImageReactiveAuBouton
         */
        public ImageReactiveAuBouton() {
            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() {
     
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Test de changement d'images dynamique");
            setAlwaysOnTop(true);
     
            jLabel1.setBackground(new java.awt.Color(254, 142, 30));
            jLabel1.setForeground(new java.awt.Color(247, 8, 8));
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gillesmaury/gbrainy.png"))); // NOI18N
            jLabel1.setLabelFor(jLabel1);
            jLabel1.setText("jLabel1");
            jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            jLabel1.setDebugGraphicsOptions(javax.swing.DebugGraphics.LOG_OPTION);
     
            jButton1.setText("jButton1");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(125, 125, 125)
                    .addComponent(jButton1)
                    .addContainerGap(203, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(83, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                        .addGap(121, 121, 121)))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(223, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(47, 47, 47))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                        .addContainerGap(96, Short.MAX_VALUE)))
            );
     
            pack();
        }// </editor-fold>
     
        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
            jLabel1.setIcon(new javax.swing.ImageIcon ("/org/gillesmaury/gbrainy.png"));
            jLabel1.setSize(10, 10);
            jLabel1.repaint();
     
    ; }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * <a href="http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html" target="_blank">http://download.oracle.com/javase/tu...feel/plaf.html</a>
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(ImageReactiveAuBouton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(ImageReactiveAuBouton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(ImageReactiveAuBouton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(ImageReactiveAuBouton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new ImageReactiveAuBouton().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private static javax.swing.JLabel jLabel1;
        // End of variables declaration
    }
    Quand je débogue avec netbean la fenêtre qui s'affiche fait disparaitre l'icone en question mais le nouveau n'apparaît pas.
    Je débute, je soupçonne qu'il faille appeler la méthode repaint du conteneur parent....

    Merci à celui qui prendra le temps de m'aider....

    @+
    Gilles

  2. #2
    Membre confirmé Avatar de maya
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2006
    Messages
    140
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2006
    Messages : 140
    Par défaut
    essaye
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
            jLabel1.setIcon(new javax.swing.ImageIcon ("/org/gillesmaury/gbrainy.png"));
            jLabel1.setSize(10, 10);
            jLabel1.updateUI();
     
    ; }

Discussions similaires

  1. [FLASH MX2004] Changer l'image d'un bouton
    Par exe dans le forum Flash
    Réponses: 3
    Dernier message: 05/12/2004, 14h26
  2. [JLabel] changer une image
    Par dadg dans le forum Composants
    Réponses: 5
    Dernier message: 28/09/2004, 10h28
  3. Comment changer l'image Bitmap en fond d'écran du bureau ?
    Par Maddelphi dans le forum API, COM et SDKs
    Réponses: 8
    Dernier message: 23/06/2004, 19h57
  4. changer l'image d'arrière plan du bureau
    Par etenclin dans le forum MFC
    Réponses: 7
    Dernier message: 22/08/2002, 15h54
  5. Changer l'image d'une texture
    Par alltech dans le forum DirectX
    Réponses: 5
    Dernier message: 21/08/2002, 01h31

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