Java Reference
In-Depth Information
The top-level containers include JApplet , JFrame , and JDialog . These
are never held inside other containers. JApplet is the Swing version of Applet .
It extends the Applet class as shown by this diagram:
java.lang.Object
| +--java.awt.Component
| +--java.awt.Container
| +--java.awt.Panel
| +--java.applet.Applet
| +--javax.swing.JApplet
The following example, which just displays a button, illustrates the essentials of
creating an instance of JApplet :
import javax.swing.*;
import java.awt.*;
/** Simple demo of adding a JComponent subclass, here a
* JButton, to the content pane.
**/
public class SwingButtonApplet extends JApplet
{
public void init () {
// Swing adds JComponents to the container's
// "content pane" rather than directly to the panel
// as with the AWT.
Container content - pane = getContentPane ();
// Create an instance of JButton
JButton button = new JButton ( " A Button " );
// Add the button to the content pane.
content - pane.add (button);
}
} // SwingButtonApplet
As shown here, the basic steps to creating a Swing interface are not very com-
plicated. Instances of components are created and then added to a container. We
add the components to a container referred to as the content pane . The top-level
Search WWH ::




Custom Search