Bonjour à tous


J'ai encore un probleme avec les fenetres mais cela concerne plus maintenant les evénements
voici mon code Code : Java
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
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
////////////////////////////////
class MaFenetre extends JFrame implements ActionListener
{
        public MaFenetre()
        {
                setTitle("Fenetre");
                setBounds(50,50,300,200);
                Container contenu = getContentPane();
                // panneau
                JPanel pan = new Panneau();
                pan.setBackground(Color.yellow);
                getContentPane().add(pan);
                // rectangle
                JButton rectangle = new JButton("Rectangle");
                contenu.add(rectangle,"North");
                rectangle.addActionListener(this);
                // ovale
                JButton ovale = new JButton("Ovale");
                contenu.add(ovale,"South");
                ovale.addActionListener(this);
        }
        public void actionPerformed (ActionEvent ev)
        {
                if(ev.getSource() == rectangle) pan.setRectangle();
                if(ev.getSource() == ovale) pan.setOvale();
                pan.repaint();
        }
        private Panneau pan;
        private JButton rectangle,ovale;
}
/////////////////////////////////
class Panneau extends JPanel
{
        public void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                if(ovale)
                {
                        g.drawOval(80,20,120,60);
                }
                if(rectangle)
                {
                        g.drawRect(80,20,120,60);
                }
        }
        public void setRectangle()
        {
                ovale = false; rectangle = true;
        }
        public void setOvale()
        {
                ovale = true; rectangle = false;
        }
        private boolean rectangle = false;
        private boolean ovale = false;
}
/////////////////////////////////
public class Fenetre
{
        public static void main(String[] args)
        {
                JFrame Fen = new MaFenetre();
                Fen.setVisible(true);
        }
}
mais il y a une erreur quand je clique sur l'un des boutons !
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
 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MaFenetre.actionPerformed(Fenetre.java:29)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Merci encore !