2 Trivial applet that displays a string - 4/96 PNL
6 import java
.awt
.event
.*;
7 import java
.applet
.Applet
;
9 import java
.net
.MalformedURLException
;
11 import netscape
.javascript
.JSObject
;
13 class AboutBox
extends Frame
{
14 AboutBox(Menu aboutMenu
, ActionListener
[] actionListeners
) {
15 super("About Applet");
19 public void windowClosing(WindowEvent e
) {
24 Button okButton
= new Button("OK");
25 okButton
.addActionListener(
26 new ActionListener() {
27 public void actionPerformed(ActionEvent e
) {
32 // Annoying use of flow layout managers.
33 Panel labelPanel
= new Panel();
34 Panel buttonPanel
= new Panel();
35 Panel fieldPanel
= new Panel();
37 labelPanel
.add(new Label("This applet's about box..."));
38 buttonPanel
.add(okButton
);
40 fieldPanel
.add(new TextField(20));
41 fieldPanel
.add(new TextField(20));
43 add(labelPanel
, "North");
44 add(buttonPanel
, "Center");
45 add(fieldPanel
, "South");
47 // test menu bar stuff.
48 MenuBar menuBar
= new MenuBar();
49 aboutMenu
= (Menu
) cloneMenu(aboutMenu
);
50 for (int i
= 0; i
< actionListeners
.length
; i
++)
51 aboutMenu
.getItem(i
).addActionListener(actionListeners
[i
]);
52 menuBar
.add(aboutMenu
);
59 private MenuItem
cloneMenu(MenuItem oldItem
) {
60 if (oldItem
instanceof Menu
) {
61 Menu oldMenu
= (Menu
) oldItem
;
62 Menu newMenu
= new Menu(oldMenu
.getLabel());
63 int count
= oldMenu
.getItemCount();
64 for (int i
= 0; i
< count
; i
++) {
65 newMenu
.add(cloneMenu(oldMenu
.getItem(i
)));
69 MenuItem newItem
= new MenuItem(oldItem
.getLabel());
75 class ExceptionThread
extends Thread
{
81 throw new Error("this is an error!");
85 public class TrivialApplet
extends Applet
{
86 public Button goButton
;
87 public Button aboutButton
;
88 public TextField urlField
;
89 public PopupMenu contextMenu
;
90 public Menu aboutMenu
;
91 public ActionListener
[] actionListeners
;
92 private static int appletCount
;
97 goButton
= new Button("Go");
98 aboutButton
= new Button("About");
100 String urlText
= getParameter("URL");
102 urlText
= "http://www.apple.com";
104 urlField
= new TextField(urlText
);
106 ActionListener goListener
=
107 new ActionListener() {
108 public void actionPerformed(ActionEvent e
) {
110 URL location
= new URL(urlField
.getText());
111 System
.out
.println("going to URL: " + location
);
112 JSObject window
= JSObject
.getWindow(TrivialApplet
.this);
113 if (window
!= null) {
114 //window.eval("alert('going to location " + location + "');");
115 window
.eval("println('" + location
+ "')");
117 getAppletContext().showDocument(location
, "_new");
118 } catch (MalformedURLException mfue
) {
123 ActionListener aboutListener
=
124 new ActionListener() {
125 public void actionPerformed(ActionEvent e
) {
126 new AboutBox(aboutMenu
, actionListeners
);
130 goButton
.addActionListener(goListener
);
131 aboutButton
.addActionListener(aboutListener
);
137 // Try a pop-up menu, and a menu in the menubar.
138 contextMenu
= new PopupMenu();
139 aboutMenu
= new Menu("About");
141 contextMenu
.add(newItem("About", aboutListener
));
142 aboutMenu
.add(newItem("About", aboutListener
));
144 contextMenu
.add(newItem("Go", goListener
));
145 aboutMenu
.add(newItem("Go", goListener
));
147 ActionListener errorListener
= new ActionListener() {
148 public void actionPerformed(ActionEvent e
) {
149 new ExceptionThread();
152 contextMenu
.add(newItem("Error", errorListener
));
153 aboutMenu
.add(newItem("Error", errorListener
));
155 ActionListener
[] listeners
= { aboutListener
, goListener
, errorListener
};
156 actionListeners
= listeners
;
160 // add a mouse listener that causes the pop-up to appear appropriately.
161 MouseListener mouseListener
= new MouseAdapter() {
162 public void mousePressed(MouseEvent e
) {
163 if (e
.isPopupTrigger()) {
165 contextMenu
.show(TrivialApplet
.this, e
.getX(), e
.getY());
170 addMouseListener(mouseListener
);
173 private MenuItem
newItem(String title
, ActionListener listener
) {
174 MenuItem item
= new MenuItem(title
);
175 item
.addActionListener(listener
);
179 private Frame
getFrame() {
181 while (p
!= null && !(p
instanceof Frame
))
186 // public void paint( Graphics g ) {}
188 public boolean mouseEnter(Event evt
, int x
, int y
) {
189 showStatus("Welcome!");
193 public boolean mouseExit(Event evt
, int x
, int y
) {
194 showStatus("See you later!");
198 public void print(String message
) {
199 JSObject window
= JSObject
.getWindow(this);
200 Object
[] args
= { message
};
201 window
.call("println", args
);
204 public int getAppletCount() {