Easy to learn
1 import java.awt.event.*;
2 import java.awt.*;
3 import java.applet.*;
4
5 public class newListenerStyle extends Applet {
6
7 public void init () {
8
9 String [] drugs = {"Amitriptyline", "Prozac", "Wellbutrin", "Desipramine", "Crack"};
10
11 //layout is very important!
12 setLayout (new GridLayout (1, drugs.length));
13
14 Button [] buttons = new Button [drugs.length];
15 for (int i = 0; i < drugs.length; i++) {
16 buttons [i] = new Button (drugs [i]);
17 final String drug = drugs [i]; // I cannot believe I have to do this
18 buttons [i].addActionListener
19 // Inline class
20 (new ActionListener () {
21 public void actionPerformed (ActionEvent e) {
22 final Frame f = new Frame (drug);
23 f.addWindowListener
24 //another inline class...
25 (new WindowAdapter () {
26 public void windowClosing (WindowEvent e2) {
27 newListenerStyle.this.showStatus ("drug");
28 f.hide ();
29 f.dispose ();
30 }
31 });
32 f.add (new Label (drug));
33 f.pack ();
34 f.show ();
35 }
36 });
37 add (buttons [i]);
38 }
39 }
40 }