Java Reference
In-Depth Information
panels in AWT, and the javax.swing.JPanel class represents panels in Swing.
Panels have the following properties:
The default layout manager of both Panel and JPanel is FlowLayout,
but since they are containers, they can have any layout manager
assigned to them.
■■
A panel can be nested within another panel, a common occurrence
when working with complex GUIs.
■■
A JPanel can take advantage of the double-buffering features in Swing,
allowing them to be updated quickly and avoid flickering.
■■
The Panel class has two constructors:
public Panel(). Creates a new Panel with FlowLayout.
public Panel(LayoutManager m).
Creates a new Panel with the specified
layout manager.
The JPanel class has four constructors:
public JPanel(). Creates a new JPanel with FlowLayout and double-
buffering turned on.
public JPanel(boolean isDoubleBuffered). Creates a new JPanel with
FlowLayout. It will use a double buffer if the boolean parameter is true.
public JPanel(LayoutManager m). Creates a new JPanel with the speci-
fied layout manager and double-buffering turned on.
public JPanel(LayoutManager m, boolean isDoubleBuffered). Creates
a new JPanel with the specified layout manager, with double-buffering
used when the boolean parameter is true.
You might be wondering what the purpose of panels is, so let's look at an
example that illustrates when a panel is useful. Suppose that you are using a
Frame with BorderLayout and you want three buttons displayed in the south.
However, you cannot add three components to the south of a BorderLayout, so
how can this be done? Well, the trick is to put a panel in the south. We can do
this because a panel is a container, and containers are also components. If the
one component we add in the south is a panel, we can add three buttons to the
panel.
The following PanelDemo program does just this. Study the program and
see if you can determine what the output will look like. Notice that the layout
manager of the Frame was not changed, so it is BorderLayout. Similarly, the
layout manager of the Panel was not changed, so it is FlowLayout by default.
The output of the PanelDemo program is shown in Figure 12.7.
Search WWH ::




Custom Search