HelloWorld2.java
 1 import java.awt.*;
 2 import java.awt.event.*;
 3 
 4 public class HelloWorld2 extends Frame implements MouseListener {
 5         public static void main(String [] args) {
 6                 new HelloWorld2();
 7         }
 8 
 9         Button b = new Button("quitter");
10         
11         Button newW = new Button("nouveau");
12         Button closeW = new Button("fermer");
13         
14         public HelloWorld2() {
15                 setLayout(new GridBagLayout());
16                 
17                 Label l = new Label("Hello World");
18                 
19                 add(b); add(newW); add(closeW);
20                 add(l);
21                 
22                 setSize(200,200);
23                 setVisible(true);
24                 
25                 b.addMouseListener(this);
26                 newW.addMouseListener(this);
27                 closeW.addMouseListener(this);
28                 
29                 pack(); // rendre la fenetre la plus petite possible en affichant tous les elements
30         }
31         
32         public void mouseClicked(MouseEvent e) {
33                 Object s = e.getSource();
34                 
35                 if(s instanceof Button) {
36                         if((Button)s == b) // quit
37                                 System.exit(0);
38                         
39                         if((Button)s == newW) // nouveau
40                                 new HelloWorld2();
41                         
42                         if((Button)s == closeW) // fermer
43                                 this.dispose();
44                 }
45         }
46         
47         public void mouseEntered(MouseEvent e) {
48         }
49         public void mouseExited(MouseEvent e) {
50         }
51         public void mousePressed(MouseEvent e) {
52         }
53         public void mouseReleased(MouseEvent e) {
54         }
55 }
56