An Applet With Panels
The applet's real estate is the area within the border
Source
AnAppletWithPanels.java:
import java.applet.*;
import java.awt.*;
public class AnAppletWithPanels extends Applet {
public void init() {
setLayout(new BorderLayout());
Panel p1 = new Panel();
p1.setBackground(Color.blue);
p1.add(new Button("1"));
p1.add(new Button("2"));
p1.add(new Button("3"));
add(p1, BorderLayout.NORTH);
Panel p2 = new Panel();
p2.setBackground(Color.yellow);
p2.add(new Button("4"));
add(p2, BorderLayout.EAST);
Panel p3 = new Panel();
p3.setBackground(Color.green);
p3.setLayout(new GridLayout(2, 0)); // 2 rows, variable columns
p3.add(new Button("5"));
p3.add(new Button("6"));
p3.add(new Button("7"));
p3.add(new Button("8"));
p3.add(new Button("9"));
add(p3, BorderLayout.CENTER);
}
}
Description and Objective
An applet that uses panels to subdivide the applet's real estate, allowing
better layout control.
- Illustrates how
Panel
s may be used to achieve better control over layout
- Illustrates uses of the
GridLayout
class
Notes
Panel
- A panel is an interface component whose sole use is to act as a container for other
components
- Panels provide the ability to group components
- Placing a Panel in a region of a BorderLayout allows multiple components to be positioned in that region
(by adding them to the Panel)
- Like Applet (n fact Applet is a form of Panel), the default layout for Panel is FlowLayout
- This default can be overridden using Panel's
setLayout
method
GridLayout
- The GridLayout class provdes a layout consisting of a rectangular grid of regions
- The number of rows/columns may be specified in the constructor
- Adding components fills the grid from left-to-right, top-to-bottom,
according to the number of rows/columns
This Applet's Behavior
The init
Method
- The applet's layout is set to BorderLayout
- Three panels are created and are placed in various regions of the applet
- Panels
p1
and p2
add some buttons to themselves
- Since no layout manager is explicitly specified, FlowLayout is used
- Panel p3 sets its layout manager to GridLayout with 2 rows and a
variable number of columns and adds some buttons
- Each panel sets its background to a different color so the extent of the panel's real estate
can be seen
- Note: In addition to allowing further subdivision of the applet's surface, Panels provide a
way to avoid the 'stretching' effect of BorderLayout
- It is the Panel thet is stretched not the contained buttons
- THis can be seen in the North and East panels
- The buttons in the center panel are stretched because GridLayout subdivides the ENTIRE
surface of the panel into the grid
- Each grid square is thus fairly large
- Each button is then stretched to the size of its grid square
Things to Do
API work
- The
Panel
class
- The
GridLayout
class
- What package is it in?
- Describe the various constructors (i.e., how do they differ)
Playing With the Applet
Code the following applet
- Reproduce the following applet that displays the right section of a keyboard
- No new layouts were introduced, but I didn't quite play fair :)