Java Notes

Containers

Top-level Containers - window, applet, dialog, ...

All elements of a user interface must be added to the content pane of a top-level container in Java 2. You can also set the content pane to a new JPanel (but not a split, tabbed, or scroll pane) with myFrame.setContentPane(myPanel), adding components to myPanel.

Use JFrame - Window for a window, and JApplet for an applet.

Another type of top-level container is a dialog, which can be created with JDialog, but it is usually easier to use Dialogs.

You will use these three types of top-level containers:

Intermediate Containers

To group components together put them on a JPanel - Container, where you can set the layout, background, and borders. You can also use a JPanel for graphics, but then don't also use it as a component container.

There are other specialized containers that act more like components, for example, JScrollPane, ... .

After you have added your components to a panel (eg, content), set the content pane of the top-level container (eg, myApplet) to this panel with myApplet.setContentPane(content);.

Here is a list of the intermediate containers, though you may never use more than JPanel.

Layouts

The way the components are arranged in a container and the way the components are rearranged when a container is resized is determined by the container's Layouts.