An Applet With Layout
The applet's real estate is the area within the border
Source
AnAppletWithLayout.java:
import java.applet.*;
import java.awt.*;
public class AnAppletWithLayout extends Applet {
public void init() {
setLayout(new BorderLayout());
Button topButton = new Button("Top");
add(topButton, BorderLayout.NORTH);
Button bottomButton = new Button("Bottom");
add(bottomButton, BorderLayout.SOUTH);
Button rightButton = new Button("Right");
add(rightButton, BorderLayout.EAST);
Button leftButton = new Button("Left");
add(leftButton, BorderLayout.WEST);
Button middleButton = new Button("Middle");
add(middleButton, BorderLayout.CENTER);
}
}
Description and Objective
An applet that uses an explicitly specified layout manager.
- Shows how to obtain better control over the positioning of the applet's interface components.
Notes
Layout
- Applets possess a default way in which components added to the applet are positioned
- Left-to-right/top-to-bottom
- This behavior can be modified by specifying an alternative
layout manager
object
for the applet.
- The layout manager object is responsible for the positioning of the components on the
applet's surface
- Each layout manager provides a different way of positioning the components
- The default layout manager for an applet is a
FlowLayout
object
BorderLayout
- The BorderLayoutManager class provdes a layout consisting of five (5) regions
- North - the upper portion of the surface
- South - the lower portion of the surface
- West - the left side of the surface
- East - the right side of the surface
- Center - the middle portion of the surface
- A slightly different form of the
add
method is used when working with
BorderLayout
- The sizing of the laid out components when using BorderLayout takes some getting used to
- Noth and South components are stretched for the entire width of the applet's surface
- East and West components are stretched for the entire height between North and South
- Center is stretched to fill in the entire middle
- Note that only 5 components can be placed when using BorderLayout
This Applet's Behavior
The init
Method
- The applet uses its
setLayout
method to change its layout manager to
a BorderLayout
object
- Five buttons are then placed in the five regions
Things to Do
API work
- The
BorderLayout
class
- What package is it in?
- Find the region constants-- what are their types?
- Find the
GridLayout
class
- What sort of layout does this class provide?
Playing With the Applet
Place the following three applets on the same page:
- Wire up the buttons to send messages to the console
- Modify AnAppletWithLayoutChange to use
FlowLayout
- Code an applet with 10 buttons positioned like a phone keypad (using
GridLayout
) --
wire them up so that when pressed they display the proper value (0-9 *, #) on the console.
(Optional- add logic to simulate dialing a phone with the following behavior:
- Pressing '#' two times in a row indicates a number is about to be dialed
- Dialed numbers must be 10 digitis preceded by a '1'
- After the 10 digit number has been dialed, a message such as 'Phone number (718) 951-5000
has been dialed."
- Once a number has been dialed pressing
*
twice terminates the call (i.e.,
displays a message "End of call").
- During a call, entering anything but '*' results in a a warning message such as "please don't touch
keypad during call" is displayed.
)