01: package example;
02:
03: import java.applet.Applet;
04: import java.awt.*;
05: import java.awt.event.*;
06:
07: /** A very simple AWT-based applet. See applet.xml and applet.hmlt.<p>
08: <blockquote><code><pre>
09: <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: } else if (ae.getSource() == lo) {
51: msg = "How low can you go?";
52: } else {
53: dialog.pack();
54: dialog.setVisible(true);
55: }
56: repaint();
57: }
58: };
59: hi.addActionListener(al);
60: lo.addActionListener(al);
61: show.addActionListener(al);
62:
63: tagThread("applet init");
64: javax.swing.SwingUtilities.invokeLater(new Runnable() {
65: public void run() {
66: tagThread("example.SimpleApplet");
67: }
68: });
69: }
70:
71: private void tagThread(String tag) {
72: Thread thread = Thread.currentThread();
73: String name = thread.getName();
74: thread.setName(name + " (" + tag + ")");
75: }
76:
77: public String getMessage() {
78: return msg;
79: }
80:
81: public void paint(Graphics g) {
82: g.drawString(getMessage(), 20, 120);
83: }
84:
85: }
|