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

avec Java Discussion :

Faire communiquer des classes entre elles?


Sujet :

avec Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 53
    Points : 29
    Points
    29
    Par défaut Faire communiquer des classes entre elles?
    Bonjour, je fais un projet de photothèque en java. Celui-ci contient des classes graphiques et des classes non graphiques qui sont toutes regroupées dans le même paquetage. Or quand je souhaite faire appel à une méthode d'une classe non graphique dans une classe graphique celle-ci n'est pas reconnue -_-' Comment puis-je y remédier?

    voici ma classe non ui:


    Code :
    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
    package PFiches;
     
     
    import java.io.*;
    import java.util.Iterator;
    import java.util.LinkedList;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
     
     
    public class Cutilisateur {
        //déclaration des attributs d'objets
        private String identifiant;
        private String mot_de_passe;
        //liste dynamique des photos de l'utilisateur
        private LinkedList<Cphoto> album=new LinkedList<Cphoto>();
        //liste dynamique des identifiants des photos
        private LinkedList<String> album_id = new LinkedList<String>();
     
        //déclaration des attributs de classe
        static private int nb_util;
        //liste des objets instanciés 
        static private LinkedList<Cutilisateur> liste_ut=new LinkedList<Cutilisateur>();
     
     
        //constructeur utilisé lors de l'execution du programme (au début)
        public Cutilisateur(String cop[]){
            //affectation des attributs à partir du tableau de données du fichier texte
            identifiant=cop[0];
            mot_de_passe=cop[1];
            for (int i=2;i<cop.length;i++){
                album_id.add(cop[i]);}
            //ajout de l'utilisateur à la liste des utilisateurs
            liste_ut.add(this);
     
        }
     
        //constructeur urilisé lors de l'inscription
        public Cutilisateur(String id, String mdp){
            //affectation des attributs de l'utilisateur à partir de son inscription
            identifiant=id;
            mot_de_passe=mdp;
            //ajout de l'utilisateur à la liste des utilisateurs
            liste_ut.add(this);
                       }
     
        //Ajout d'un utilisateur dans la liste des membres
        public void ajout_ut(){
            liste_ut.add(this);
      }
     
        //retrait d'un utilisateur de la liste des membres
        public void retrait_ut(){
            liste_ut.remove(this);
        }
     
        //affichage des caractéristiques de l'utilisateur
        public void afficher_infos(){
            System.out.println("identifiant : " + identifiant);
            System.out.println("mot de passe : " + mot_de_passe);
            System.out.println("identifiant des photos : " + album_id);
        }
     
     
        //affichage de la liste de sutilisateurs
        static public void afficher_liste(){
            //déclaration et initialisation d'un itérateur pour parcourir la liste
            Iterator  iter =liste_ut.iterator();
            // tant que toute la liste n'a pas été parcourue, 
            while (iter.hasNext() ){
               Cutilisateur ut =(Cutilisateur) iter.next();
               //affichage des informations de chaque utilisateur
               ut.afficher_infos();
               }
           }
     
        //lecture du fichier contenant les informations de l'utilisateur 
        static void lecture_ut() throws FileNotFoundException, IOException{
        //précision du fichier texte concerné
        String nom_fich="util.txt";
        FileReader fichier = new FileReader (nom_fich); 
        BufferedReader in  = new BufferedReader(fichier);
        String ligne;
        //on commence la lecture à la premiere ligne du fichier
        ligne = in.readLine();
        //tant que l'on n'a pas parcouru tout le fichier, 
        while(ligne!= null){
           String infos[];
           //on découpe la ligne selon les espaces 
           //on remplit le tableau infos avec les mots de la ligne
           infos = ligne.split(" ");
           //instanciation de la classe d'utilisateur
           Cutilisateur ut = new Cutilisateur(infos);
           //on passe à la ligne suivante
           ligne=in.readLine();       
            }
    //test de la procédure  
    // Cutilisateur.afficher_liste();
    //fermeture du fichier
     fichier.close();
    }
     
        //procédure d'écriture du fichier 
        static void enregistrement_fich()throws FileNotFoundException, IOException{
        //création d'un nouveau fichier texte    
        File fichier = new File("util.txt");
        FileWriter out = new FileWriter(fichier);
        //instanciation d'un itérateur pour parcourir la liste d'utilisateurs
        Iterator iter = liste_ut.iterator();
        //tant que l'on n'a pas parcouru toute la liste d'utilisateurs
        while (iter.hasNext()){
            Cutilisateur ut =(Cutilisateur) iter.next();
            //écriture dans le fichier
            out.write(ut.identifiant+" "+ ut.mot_de_passe);
            //instanciation d'un itérateur pour parcourir la liste des identifiants de photos
            Iterator iter2 = ut.album_id.iterator();
            //tant que l'on a pas parcouru la liste
            while (iter2.hasNext()){
                String photo = (String)iter2.next();
                //écriture dans le fichier
                out.write(" "+photo);
            }
            //passage à la ligne dans le fichier
           out.write("\r\n");
        }
        //fermeture du fichier
        out.close();
    }
     
        static public boolean verif_connexion(String identifiant, String mdp){
            Iterator iter = liste_ut.iterator();
            while (iter.hasNext()){
                Cutilisateur ut=(Cutilisateur) iter.next();
                if ((ut.identifiant==identifiant)&&(ut.mot_de_passe==mdp)){
                    return(true);
                }
            }
            return(false);
        }
     
       static public  boolean verif_pseudo_inscription(String pseudo_choisit){
            Iterator iter = liste_ut.iterator();
            while (iter.hasNext()){
                Cutilisateur ut=(Cutilisateur) iter.next();
                if(ut.identifiant==pseudo_choisit){
                    return(false);
                }
            }
            return(false);
        }
     
       static public LinkedList<Cutilisateur> get_liste_ut(){
       return(liste_ut);
       }
     
    }
    et ma classe graphique :


    Code :

    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
    /*
     * CFAccueil.java
     *
     * Created on 11 février 2009, 11:00
     */
     
    package PFiches;
     
     
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
     
    /**
     *
     * @author  Fanny
     */
    public class CFAccueil extends javax.swing.JFrame {
     
        CFMenu FMenu;
        CFCréerProfil FCréerProfil;
        CFRechercheMC FRechercheMC;
     
     
     
        /** Creates new form CFAccueil */
        public CFAccueil() throws FileNotFoundException, IOException {
            initComponents();
            FCréerProfil = new CFCréerProfil(this,false);
            FRechercheMC = new CFRechercheMC(this,false);
            FMenu = new CFMenu(this,false);
            //Lecture du fichier       
            Cutilisateur.lecture_ut();
     
     
        }
     
        /** 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() {
     
            lTexteaccueil = new javax.swing.JLabel();
            tfNom = new javax.swing.JTextField();
            lTexteaccueil1 = new javax.swing.JLabel();
            pfMotdePasse = new javax.swing.JPasswordField();
            lMotdePasse = new javax.swing.JLabel();
            bOK = new javax.swing.JButton();
            bQuitter = new javax.swing.JButton();
            bCréationProfil = new javax.swing.JButton();
            jSeparator1 = new javax.swing.JSeparator();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Accueil");
            setBackground(java.awt.Color.blue);
     
            lTexteaccueil.setFont(new java.awt.Font("Tahoma", 0, 24));
            lTexteaccueil.setText("Bienvenue sur Mezimages");
     
            tfNom.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tfNomActionPerformed(evt);
                }
            });
     
            lTexteaccueil1.setText("Entrez votre identifiant :");
     
            lMotdePasse.setText("Entrez votre mot de passe :");
     
            bOK.setText("OK");
            bOK.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bOKActionPerformed(evt);
                }
            });
     
            bQuitter.setText("Quitter");
            bQuitter.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bQuitterActionPerformed(evt);
                }
            });
     
            bCréationProfil.setText("Créer un profil");
            bCréationProfil.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bCréationProfilActionPerformed(evt);
                }
            });
     
            jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
     
            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(56, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(lTexteaccueil)
                            .addGap(61, 61, 61))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(bOK)
                                    .addGap(49, 49, 49))
                                .addComponent(lTexteaccueil1)
                                .addComponent(tfNom, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                                .addComponent(lMotdePasse)
                                .addComponent(pfMotdePasse, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(117, 117, 117)
                                    .addComponent(bQuitter)
                                    .addContainerGap())
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(bCréationProfil)
                                    .addGap(48, 48, 48))))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(36, 36, 36)
                    .addComponent(lTexteaccueil, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(81, 81, 81)
                            .addComponent(bCréationProfil))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addComponent(lTexteaccueil1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tfNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32)
                            .addComponent(lMotdePasse)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(pfMotdePasse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(bOK)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                            .addComponent(bQuitter))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
    private void bOKActionPerformed(java.awt.event.ActionEvent evt) {                                    
     
        verif_connexion(tfNom.getText(),pfMotdePasse.getPassword().toString() );   
        this.setVisible(false);
        FMenu.setVisible(true);
     
    }                                   
     
    private void tfNomActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    }                                     
     
    private void bQuitterActionPerformed(java.awt.event.ActionEvent evt) {                                         
            try {
                Cutilisateur.enregistrement_fich();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(CFAccueil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(CFAccueil.class.getName()).log(Level.SEVERE, null, ex);
            }
        System.exit(0);
    }                                        
     
    private void bCréationProfilActionPerformed(java.awt.event.ActionEvent evt) {                                                
    this.setVisible(false);
     FCréerProfil.setVisible(true);
    }                                               
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        new CFAccueil().setVisible(true);
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(CFAccueil.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(CFAccueil.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton bCréationProfil;
        private javax.swing.JButton bOK;
        private javax.swing.JButton bQuitter;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JLabel lMotdePasse;
        private javax.swing.JLabel lTexteaccueil;
        private javax.swing.JLabel lTexteaccueil1;
        private javax.swing.JPasswordField pfMotdePasse;
        private javax.swing.JTextField tfNom;
        // End of variables declaration                   
     
     
     
    }
    la méthode qui cause problème est verif_connexion (j'y fait appel vers la fin de ma classe graphique) qui n'est pas reconnue lorsque j'y fais appel dans ma classe graphique.

    Merci beaucoup pour votre aide

  2. #2
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 1
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    il y a deux solutions a ton problème

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Cutilisateur.verif_connexion(...);
    ou

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    import static PFiches.Cutilisateur.*;
    La dernière solution import l'ensemble de tes méthodes statique de ta classe Cutilisateur

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 53
    Points : 29
    Points
    29
    Par défaut
    J'avais carrément oublié qu'il fallait préciser la classe quand on utilisait une méthode statique! Merci beaucoup!!

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

Discussions similaires

  1. Réponses: 9
    Dernier message: 27/02/2011, 14h49
  2. Faire communiquer 2 servlet entre elles
    Par gamiel dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 04/04/2010, 12h16
  3. Faire rebondir des boules entre elles
    Par scheme dans le forum Tkinter
    Réponses: 2
    Dernier message: 11/09/2008, 14h37
  4. Utilisation des classes entre elles
    Par leodi dans le forum Flash
    Réponses: 1
    Dernier message: 17/05/2008, 16h20
  5. Gestion des classes entre elles et organisation.
    Par Hybrix dans le forum Développement 2D, 3D et Jeux
    Réponses: 54
    Dernier message: 29/08/2007, 19h54

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