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 :

Probleme JPanel


Sujet :

AWT/Swing Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 54
    Points : 27
    Points
    27
    Par défaut Probleme JPanel
    Coucou

    Alors voila le probleme, je viens de creer une petite interface graphique grace au GUI de NetBeans et maintenant je suis bien embarasser :

    J'ai une classe qui etant JFrame, cette classe possede un JPanel, qui est initialiser par une classe qui etends JPanel. Jusque la ca marche, j'ai bien mon panel dans ma JFrame et tout.

    Sur ce panel, j'ai plusieur bouton. Lorsque je clique sur l'un d'eux, je veut faire un appel a une méthode qui etends Jpanel egalement. Je fait donc : -clique sur un bouton
    -removeAll pour tout enlever
    -ensuite je fait repaint pour mettre a jour
    -j'appelle une methode qui me retourne un panel
    -je l'ajoute a mon panel
    -je fait revalidate pour qu'il prenne en compte les modif


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    public void mouseClicked(MouseEvent e) {
            if (e.getSource() == jButton6){ // creation de partie
                this.removeAll();
                this.repaint();
                Methode m= new Methode ();
                m.initButton();
                this.add(m);
                this.revalidate();
            }
    }
    Amis voila ca marche pas, ca me met un panel vide alors si vous avez une idée parce que la je seche...

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    128
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 128
    Points : 151
    Points
    151
    Par défaut
    Pour commencer, utilises plutot un ActionListener à la place du MouseListener (c'est fait justement pour gérer les évenements de type bouton, liste etc).

    Ensuite, difficile de dire où se situe ton problème sans voir un peu plus de code. Comment est fait le panel retourné par Methode() (en particulier en terme de layout, taille, positionnement...)? Quel est le layout de ton panel initial?

  3. #3
    Membre actif Avatar de schneidb
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    236
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 236
    Points : 240
    Points
    240
    Par défaut
    as-tu bien ajouté ton écouteur sur ton panel via un addMouseListener ou addActionListener ?

  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
    salut

    as tu verifier avec des logs que le panel qui est retourné (m?) n'est pas null ou vide ?
    si 'this' est ta JFrame il ne faut pas faire
    mais
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    this.getContentPane().add(m);

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 54
    Points : 27
    Points
    27
    Par défaut
    alors en fait J'ai une JFrame qui appel une methode qui etend Jpanel qui elle meme appel JPanel

    Voici mes 3 classes

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    import java.awt.*;
    import javax.swing.*;
     
    public class WindowGame extends JFrame{
        MenuPrincipal panel;
        public WindowGame() {
            super("Projet Pluri"); // appel du super constructeur
            panel = new MenuPrincipal();
            panel.initButton();
            this.getContentPane().add(panel);
        }    
    }
    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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
     
    import javax.swing.*;
    import java.awt.event.* ;
    import java.io.*;
    public class MenuPrincipal extends JPanel implements MouseListener{
     
        /** Creates new form Panel */
        public MenuPrincipal() {
            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() {
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
            jButton6 = new javax.swing.JButton();
            jButton7 = new javax.swing.JButton();
            jButton8 = new javax.swing.JButton();
            jButton9 = new javax.swing.JButton();
            jButton10 = new javax.swing.JButton();
            jButton11 = new javax.swing.JButton();
     
            jButton1.setText("Jouer");
            jButton1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton2.setText("Options");
            jButton2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton3.setText("Gestion");
            jButton3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton4.setText("Aide");
            jButton4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton5.setText("Quitter");
            jButton5.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton6.setText("Creer partie");
            jButton6.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton7.setText("Rejoinde partie");
            jButton7.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton8.setText("Ajout d'un jeu");
            jButton8.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton9.setText("MAJ d'un jeu");
            jButton9.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton10.setText("Oui");
            jButton10.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton11.setText("Non");
            jButton11.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(162, 162, 162)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                        .add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                        .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jButton11, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .add(jButton7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .add(jButton8, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .add(jButton9, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .add(jButton10, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .add(jButton6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
                    .add(84, 84, 84))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                    .add(77, 77, 77)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton6))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                        .add(jButton7))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                        .add(jButton8))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                        .add(jButton9))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                        .add(jButton10))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jButton11)
                    .add(59, 59, 59))
            );
        }// </editor-fold>                        
     
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton10;
        private javax.swing.JButton jButton11;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JButton jButton7;
        private javax.swing.JButton jButton8;
        private javax.swing.JButton jButton9;
        // End of variables declaration                   
     
        public void initButton(){
            init();
            jButton1.addMouseListener(this);
            jButton2.addMouseListener(this);
            jButton3.addMouseListener(this);
            jButton4.addMouseListener(this);
            jButton5.addMouseListener(this);
            jButton6.addMouseListener(this);
            jButton7.addMouseListener(this);
            jButton8.addMouseListener(this);
            jButton9.addMouseListener(this);
            jButton10.addMouseListener(this);
            jButton11.addMouseListener(this);
     
        }
        public void init(){
            jButton1.setVisible(true);
            jButton2.setVisible(true);
            jButton3.setVisible(true);
            jButton4.setVisible(true);
            jButton5.setVisible(true);
            jButton6.setVisible(false);
            jButton7.setVisible(false);
            jButton8.setVisible(false);
            jButton9.setVisible(false);
            jButton10.setVisible(false);
            jButton11.setVisible(false);
        }
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            if (e.getSource() == jButton6){ // creation de partie
                this.removeAll();
                this.repaint();
                CreateGame cg = new CreateGame();
                cg.initButton();
                this.add(cg);
                this.repaint();
            }else if (e.getSource() == jButton7){ // rejoindre une partie
                this.removeAll();
                this.repaint();
                JoinGame jg = new JoinGame();
                //jg.initButton();
                this.add(jg);
                this.revalidate();
            }else if (e.getSource() == jButton2){ // rejoindre une partie
                this.removeAll();
                this.repaint();
                Option o = new Option();
                //jg.initButton();
                this.add(o);
                this.revalidate();
            }else if (e.getSource() == jButton8){ // rejoindre une partie
                this.removeAll();
                this.repaint();
                AddGame ag = new AddGame();
                //jg.initButton();
                this.add(ag);
                this.revalidate();
            }else if (e.getSource() == jButton9){ // rejoindre une partie
                this.removeAll();
                this.repaint();
                UpdateGame ug = new UpdateGame();
                //jg.initButton();
                this.add(ug);
                this.revalidate();
            }else if (e.getSource() == jButton4){ // rejoindre une partie
                try{
                    BrowserLauncher.openURL("./src/games/corps.html");
                }catch(IOException ioe){System.out.println(ioe);}
            }
            else if (e.getSource() == jButton10){ // rejoindre une partie
                //System.out.println("exit");
                System.exit(0);
            }else if (e.getSource() == jButton11){ // rejoindre une partie
                init();
            }
        }
        public void mouseEntered(MouseEvent e) {
            if (e.getSource() == jButton1){
                init();
                jButton6.setVisible(true);
                jButton7.setVisible(true);
            } else if (e.getSource() == jButton2){
                init();
            }else if (e.getSource() == jButton3){
                init();
                jButton8.setVisible(true);
                jButton9.setVisible(true);
     
            }else if (e.getSource() == jButton4){
                init();
            }else if (e.getSource() == jButton5){
                init();
                jButton10.setVisible(true);
                jButton11.setVisible(true);
            }}
        public void mouseExited(MouseEvent e) {}
    }
    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
     
    import javax.swing.*;
    public class CreateGame extends JPanel {
     
        /** Creates new form CreateGame */
        public CreateGame() {
            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();
            jLabel2 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
     
            setBackground(new java.awt.Color(255, 255, 255));
            jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setText("Liste des jeux disponibles");
     
            jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 24));
            jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel2.setText("Nombre de joueur");
     
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
            jComboBox1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
     
            jButton1.setText("Quitter");
            jButton1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            jButton2.setText("Valider");
            jButton2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                    .add(72, 72, 72)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .add(15, 15, 15)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)))
                    .add(68, 68, 68))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                    .add(70, 70, 70)
                    .add(jLabel1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 8, Short.MAX_VALUE)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jLabel2)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(20, 20, 20)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton1)
                        .add(jButton2))
                    .add(92, 92, 92))
            );
        }// </editor-fold>                        
     
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
        public void initButton(){
            jButton1.setVisible(true);
        }
    }

    voila j'espere que vous pourrez m'aider

    D'avance merci

Discussions similaires

  1. probleme jpanel java
    Par mr-nameless dans le forum Débuter
    Réponses: 1
    Dernier message: 01/04/2010, 11h22
  2. Problem JPanel plantage
    Par Nabil_z dans le forum Agents de placement/Fenêtres
    Réponses: 0
    Dernier message: 25/03/2009, 15h57
  3. probleme JPanel dans JScrollPane
    Par hbar01 dans le forum Agents de placement/Fenêtres
    Réponses: 6
    Dernier message: 30/05/2008, 19h37
  4. probleme JPanel + scroll
    Par Panther.I dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 02/05/2007, 10h47
  5. probleme JPanel et graphics
    Par jayjay.f dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 21/03/2007, 18h22

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