Bonjour,
j'essaye de créer mon premier programme java qui n'aura aucun intérêt mais je bloque sur l'insertion d'une image dans un JPanel.
Voici mon code :
Ca compile et tous mais quand cette fenetre est censée s'ouvrir il ne se passe rien et eclipse me sort tout un tas d'erreurs :
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 import javax.swing.*; import java.awt.*; public class popupinfo extends JFrame { private JPanel zone; private JPanel zoneimg; private JLabel message; private ImageIcon image; public popupinfo(String nomFenetre, int x, int y) { // Appelle du constructeur super(nomFenetre); setSize(x, y); // Placement de la fenetre Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); setLocation(dim.width/2 - getWidth()/2, dim.height/2 - getHeight()/2); // Caractéristiques zone = new JPanel(); getContentPane().add(zone); // Message message = new JLabel("Programme développé par Sylvain R."); message.setHorizontalAlignment(JTextField.CENTER); message.setVerticalAlignment(JTextField.CENTER); message.setSize(zone.getWidth(), zone.getHeight()); // Image ImageIcon image = new ImageIcon("elin.jpg"); zoneimg.getGraphics().drawImage(image.getImage(), 0, 0, zoneimg); // Définition des objets utilisés GridBagLayout placeur = new GridBagLayout(); GridBagConstraints contraintes = new GridBagConstraints(); getContentPane().setLayout(placeur); contraintes.fill = GridBagConstraints.BOTH; contraintes.insets = new Insets(1, 1, 1, 1); // placement du composant du message contraintes.gridx = 1; contraintes.gridy = 1; contraintes.gridwidth = 1; contraintes.gridheight = 1; contraintes.weightx = 1; contraintes.weighty = 1; placeur.setConstraints(message, contraintes); getContentPane().add(message); //placement de l'image contraintes.gridx = 0; contraintes.gridy = 0; contraintes.gridwidth = 1; contraintes.gridheight = 1; contraintes.weightx = 1; contraintes.weighty = 1; placeur.setConstraints(zoneimg, contraintes); getContentPane().add(zoneimg); // Rendre la fenetre visible setVisible(true); } }
Ce que je ne comprends pas c'est que le fichier "elin.jpg" est bien présent dans le meme repertoire que le .class qui contient la méthode main.Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at popupinfo.<init>(popupinfo.java:33)
at fenetre$1.actionPerformed(fenetre.java:113)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Un petit coup de main serait le bien venu.
Merci.






Répondre avec citation



Ce truc je l'utilise une fois par an et je me fais avoir à chaque fois. Oui il faut passer par un JLabel.
Partager