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 :

Erreur "static reference to the non-static field"


Sujet :

AWT/Swing Java

  1. #1
    Membre régulier Avatar de NetLandGim
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations forums :
    Inscription : Mars 2011
    Messages : 97
    Points : 113
    Points
    113
    Par défaut Erreur "static reference to the non-static field"
    Bonjour,

    J'ai un problème de Static et non-Static.

    Je viens de créer ma première fenêtre par programmation.

    Voici le 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
    package intro;
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class MaFenetre {
     
    	public  static void main (String [] args){
     
    		unPanneau = new JTextField();
    		mesBoutons= new JButton[9];
     
    		for (int i=0;mesBoutons.length<9;i++){
     
    			mesBoutons[i] = new JButton("B"+i);
    			mesBoutons[i].setText(Integer.toString(i));
    			btnPanel.add(mesBoutons[i]);
     
    			mesBoutons[i].addMouseListener(new MouseAdapter(){
     
    				public void mouseReleased(MouseEvent evt){
     
    					afficheMouseReleased(evt);
    				}
    			});
    		}
     
    		GridLayout gridLayout =new GridLayout(3,3);
    		btnPanel.setLayout(gridLayout);
    		btnPanel.validate();
    		panPanel.add(unPanneau);
    		panPanel.setLayout(null);
    		panPanel.validate();
    		pPanel.add(btnPanel);
    		pPanel.add(panPanel);
    		pPanel.setLayout(new GridLayout(1,1));
    		pPanel.validate();
    		frame.setContentPane(pPanel);
    		frame.setSize(400,300);
    		frame.setTitle("Ma première Fenetre");
    		frame.setVisible(true);
     
    	}
     
    	private void afficheMouseReleased(MouseEvent evt){
     
    		JButton Boutton=(JButton) evt.getSource();
    		unPanneau.setText("Vous avez cliqué sur :"+Boutton.getText());
    	}	
     
    	private JButton [] mesBoutons;
    	private JTextField unPanneau;
    	private JPanel btnPanel=new JPanel();
    	private JPanel panPanel=new JPanel();
    	private JPanel pPanel =new JPanel();
    	private JFrame frame=new JFrame();
    }
    Mais Éclipse trouve lors de la compilation une erreur:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot make a static reference to the non-static field unPanneau
    Cannot make a static reference to the non-static field mesBoutons
    Cannot make a static reference to the non-static field mesBoutons
    Cannot make a static reference to the non-static field mesBoutons
    Cannot make a static reference to the non-static field btnPanel
    Cannot make a static reference to the non-static field mesBoutons
    Cannot make a static reference to the non-static field mesBoutons
    Cannot make a static reference to the non-static method afficheMouseReleased(MouseEvent) from the type MaFenetre
    Cannot make a static reference to the non-static field btnPanel
    Cannot make a static reference to the non-static field btnPanel
    Cannot make a static reference to the non-static field panPanel
    Cannot make a static reference to the non-static field unPanneau
    Cannot make a static reference to the non-static field panPanel
    Cannot make a static reference to the non-static field panPanel
    Cannot make a static reference to the non-static field pPanel
    Cannot make a static reference to the non-static field btnPanel
    Cannot make a static reference to the non-static field pPanel
    Cannot make a static reference to the non-static field panPanel
    Cannot make a static reference to the non-static field pPanel
    Cannot make a static reference to the non-static field pPanel
    Cannot make a static reference to the non-static field frame
    Cannot make a static reference to the non-static field pPanel
    Cannot make a static reference to the non-static field frame
    Cannot make a static reference to the non-static field frame
    Cannot make a static reference to the non-static field frame

    at intro.MaFenetre.main(MaFenetre.java:19)
    Merci pour votre aide

  2. #2
    En attente de confirmation mail
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Octobre 2010
    Messages
    501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Octobre 2010
    Messages : 501
    Points : 1 060
    Points
    1 060
    Par défaut
    Bonsoir,

    La méthode main est static, cela signifie qu'elle est exécutée sans instanciation d'un objet. Ainsi les propriétés non statiques n'existent pas.
    Une solution est de rendre static les propriétés nécessaires à la classe main.

  3. #3
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    La solution propre est de ne surtout pas coder votre programme dans le main. Si les classes existent c'est qu'elles ont une utilité. Programmer dans le main revient à se couper de tout les avantages de la programmation objet...

    De même, hors cas très précis (constantes, méthodes utilitaires pures, patterns spécifiques), on évite d'utiliser le mot clef static. En effet le static fait sortir la programmation du paradygme objet...
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

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

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

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    déplacez tout le code de votre main dans le constructeur de MaFenetre, par exemple. Et mettez comme seul code dans votre main


  5. #5
    Membre régulier Avatar de NetLandGim
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations forums :
    Inscription : Mars 2011
    Messages : 97
    Points : 113
    Points
    113
    Par défaut
    En suivant vos conseils , voici ce que j'obtiens et qui marche

    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
    package intro;
    import java.awt.BorderLayout;
    		import java.awt.Dimension;
    		import java.awt.GridLayout;
    		import java.awt.event.MouseAdapter;
    		import java.awt.event.MouseEvent;
     
    		import javax.swing.JButton;
    		import javax.swing.JFrame;
    		import javax.swing.JPanel;
    		import javax.swing.JTextField;
    public class MaFenetre {
     
     
    	public MaFenetre(){
    		unPanneau = new JTextField();
    		mesBoutons= new JButton[9];
     
    		for (int i=0;i<mesBoutons.length;i++){
     
    			mesBoutons[i] = new JButton("B "+i);
    			//mesBoutons[i].setText(Integer.toString(i));
    			btnPanel.add(mesBoutons[i]);
     
    			mesBoutons[i].addMouseListener(new MouseAdapter(){
     
    				public void mouseReleased(MouseEvent evt){
     
    					afficheMouseReleased(evt);
    				}
    			});
    		}
     
    		GridLayout gridLayout =new GridLayout(3,3);
    		btnPanel.setLayout(gridLayout);
    		btnPanel.validate();
    		panPanel.add(unPanneau);
    		panPanel.setLayout(new GridLayout());
    		panPanel.validate();
    		pPanel.add(btnPanel);
    		pPanel.add(panPanel);
    		pPanel.setLayout(new GridLayout(1,1));
    		pPanel.validate();
    		frame.setContentPane(pPanel);
    		frame.setSize(400,300);
    		frame.setTitle("Ma première Fenetre");
    		frame.setVisible(true);
     
    	}
     
    	private void afficheMouseReleased(MouseEvent evt){
     
    		JButton Boutton=(JButton) evt.getSource();
    		unPanneau.setText("Vous avez cliqué sur :"+Boutton.getText());
    		frame.setTitle("Vous avez cliqué sur :"+Boutton.getText());
    	}
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    new MaFenetre();
    	}
    	private JButton [] mesBoutons;
    	private JTextField unPanneau;
    	private JPanel btnPanel=new JPanel();
    	private JPanel panPanel=new JPanel();
    	private JPanel pPanel =new JPanel();
    	private JFrame frame=new JFrame();
     
    }
    Merci

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 26/01/2012, 14h53
  2. Réponses: 1
    Dernier message: 24/05/2011, 23h49
  3. Cannot make a static reference to the non-static method readValues
    Par delta07 dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 30/06/2010, 15h14
  4. Réponses: 5
    Dernier message: 18/05/2007, 12h30
  5. Cannot make a static reference to the non-static method
    Par semaj_james dans le forum Langage
    Réponses: 5
    Dernier message: 11/05/2006, 23h10

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