Bonjour ,
Je cherche à faire une appli qui lorsque l'on clique sur la fenetre , cela change d'image .
Donc je lance l'appli , il y a la 1ere image , mais lorsque je clic , cela me dit :
Quelqu'un saurait me dire d'ou vient l'erreur svp ?
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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Femme.changeIcon(Femme.java:34) at Femme.access$000(Femme.java:9) at Femme$1.mouseClicked(Femme.java:28) at java.awt.Component.processMouseEvent(Component.java:6385) at javax.swing.JComponent.processMouseEvent(JComponent.java:3275) at java.awt.Component.processEvent(Component.java:6147) at java.awt.Container.processEvent(Container.java:2083) at java.awt.Component.dispatchEventImpl(Component.java:4744) at java.awt.Container.dispatchEventImpl(Container.java:2141) at java.awt.Component.dispatchEvent(Component.java:4572) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4289) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210) at java.awt.Container.dispatchEventImpl(Container.java:2127) at java.awt.Window.dispatchEventImpl(Window.java:2489) at java.awt.Component.dispatchEvent(Component.java:4572) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710) at java.awt.EventQueue.access$400(EventQueue.java:82) at java.awt.EventQueue$2.run(EventQueue.java:669) at java.awt.EventQueue$2.run(EventQueue.java:667) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue$3.run(EventQueue.java:683) at java.awt.EventQueue$3.run(EventQueue.java:681) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:680) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
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 import java.awt.*; import javax.swing.*; import java.awt.event.*; /* Pour détecter les événements qui surviennent sur notre composant , Javautilise ce que l'on appelle le design pattern observer */ /* addMouseLister(MouseListener obj) prend un objet Mouselistener en parametre ( prendra this ) */ public class Femme { private static final String path1="/Users/rebouhaymen/Desktop/Aymen/IUT/Java/TP2/img1.png"; private static final String path2="/Users/rebouhaymen/Desktop/Aymen/IUT/Java/TP2/img2.png"; private static final String path3="/Users/rebouhaymen/Desktop/Aymen/IUT/Java/TP2/img3.png"; private JFrame jframe; private ImageIcon[] arrayII; public Femme() { jframe=new JFrame("Aymen's works"); jframe.setLayout(new BorderLayout()); ImageIcon img1=new ImageIcon(path1) ;ImageIcon img2=new ImageIcon(path2);ImageIcon img3=new ImageIcon(path3); final ImageIcon arrayII[]=new ImageIcon [5]; arrayII[0]=img1; arrayII[1]=img2; arrayII[2]=img3; final JLabel jlabel = new JLabel(arrayII[0],JLabel.CENTER); jframe.setSize(500,500); jlabel.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent me){ changeIcon(); jlabel.setIcon(arrayII[1]); }} ); jframe.add(jlabel, BorderLayout.CENTER); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true); } private void changeIcon() { arrayII[0]=(arrayII[0]==arrayII[1] ? arrayII[1] : arrayII[2]);} public static void main(String[] args) { Femme fem=new Femme();} }
Partager