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:
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:
Citation:
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