Java Reference
In-Depth Information
The following ContextDemo applet demonstrates using some of the meth-
ods in the AppletContext. The events of the two buttons are handled by the
ShowDocument class, which also follows. Study the classes, and try to deter-
mine how the applet looks and what it does.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class ContextDemo extends JApplet
{
private JButton show1, show2;
private JTextField url;
private AppletContext context;
public void init()
{
context = this.getAppletContext();
Container contentPane = this.getContentPane();
url = new JTextField(30);
show1 = new JButton(“Show”);
show2 = new JButton(“Open in new window”);
contentPane.add(new JLabel(“Enter a URL:”,
SwingConstants.CENTER), BorderLayout.NORTH);
JPanel center = new JPanel();
center.add(url);
contentPane.add(center, BorderLayout.CENTER);
JPanel south = new JPanel();
south.add(show1);
south.add(show2);
contentPane.add(south, BorderLayout.SOUTH);
//Register event handlers.
ShowDocument listener = new ShowDocument(context, url);
show1.addActionListener(listener);
show2.addActionListener(listener);
}
public void start()
{
context.showStatus(“ContextDemo has started!”);
}
}
import java.awt.event.*;
import java.applet.AppletContext;
import java.net.*;
import javax.swing.JTextField;
public class ShowDocument implements ActionListener
{
private AppletContext context;
private JTextField textField;
Search WWH ::




Custom Search