BorderLayout
and put the TextArea
in its center. Then we create a Panel
, set the LayoutManager
of the Panel
to FlowLayout
, add the button to the panel, and then add the panel to the south part of the applet. Indeed that's exactly what was done to produce the above applet. Here's the code:
import java.applet.*;
import java.awt.*;
public class PanelExample extends Applet {
public void init() {
this.setLayout(new BorderLayout());
this.add("Center", new TextArea());
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.add(new Button("OK"));
this.add("South", p);
}
}
It's important in this example to distinguish between adding to the applet (add(...)
or this.add(...)
) and adding to the panel (p.add(...)
).On the other hand it doesn't matter whether you add the panel to the applet and then add the button to the panel, or first add the button to the panel and then add the panel to the applet.
Another common use for a panel is to align a series of checkboxes in a GridLayout
with one column.