1 package example;
2
3 import java.applet.Applet;
4 import java.awt;
5 import java.awt.event;
6
7 /** A very simple AWT-based applet. See applet.xml and applet.hmlt.<p>
8 <blockquote><code><pre>
9 <applet code="example.SimpleApplet" width=250 height>
10 </applet>
11 </pre></code></blockquote>
12 <p>
13 @author kelvinr@users.sourceforge.net, twall@users.sourceforge.net
14 */
15
16 public class SimpleApplet extends Applet {
17
18 String msg = "This is a simple applet";
19
20 public void init() {
21 Label push = new Label("Press a button");
22 final Button hi = new Button("High");
23 final Button lo = new Button("Low");
24 final Button show = new Button("?");
25 Component parent = this;
26 while (parent.getParent() != null) {
27 parent = parent.getParent();
28 }
29 if (!(parent instanceof Frame))
30 parent = new Frame("Dummy Frame");
31 final Dialog dialog = new Dialog((Frame)parent, "Dialog", true);
32 dialog.add(new Label("This is a dialog"));
33 dialog.addWindowListener(new WindowAdapter() {
34 public void windowClosing(WindowEvent we) {
35 we.getWindow().setVisible(false);
36 }
37 });
38
39 // Adds labels and buttons to applet window
40 add(push);
41 add(hi);
42 add(lo);
43 add(show);
44 add(new TextField("text here"));
45
46 ActionListener al = new ActionListener() {
47 public void actionPerformed(ActionEvent ae) {
48 if (ae.getSource() == hi) {
49 msg = "Up, up and away!";
50 }
51 else if (ae.getSource() == lo) {
52 msg = "How low can you go?";
53 }
54 else {
55 dialog.pack();
56 dialog.setVisible(true);
57 }
58 repaint();
59 }
60 };
61 hi.addActionListener(al);
62 lo.addActionListener(al);
63 show.addActionListener(al);
64
65 tagThread("applet init");
66 javax.swing.SwingUtilities.invokeLater(new Runnable() {
67 public void run() {
68 tagThread("example.SimpleApplet");
69 }
70 });
71 }
72
73 private void tagThread(String tag) {
74 Thread thread = Thread.currentThread();
75 String name = thread.getName();
76 thread.setName(name + " (" + tag + ")");
77 }
78
79 public String getMessage() {
80 return msg;
81 }
82
83 public void paint(Graphics g) {
84 g.drawString(getMessage(), 20, 120);
85 }
86
87 }
88