Activity 6.1 - Constructing a frame with buttons using BorderLayout
Topic
In this activity we display a frame with buttons arranged using BorderLayout,
which is the default layout for a JFrame.
Here is a picture of what the frame will look like.

Task
The task is to display a frame with the following attributes:
- Its top left-hand corner is to be at screen location (200, 200). (The default
position of the frame would be at (0, 0), with
its top left corner at the top
left of the computer screen.)
- Its dimensions are to be 210 x 297, in proportion to a page of A4
paper.
- The title is to be "A4 Page".
- The frame is to have buttons at the top, bottom, left and right,
corresponding to the margins of the page. The text of the buttons is to
be
"Top", "Bottom", "Left", "Right" respectively.
You will need to write a Java class to the following outline:
Imports
AWT and Swing.
Inheritance
Extends JFrame.
Instance variables
Four fields of type JButton to represent
the buttons.
Constructor
Takes a single String argument used to set
the title of the frame.
- Sets the title.
- Sets the size to 210 x 297.
- Positions the top left-hand corner of the frame at screen
location (200,
200). This can be done using the method setLocation(int
x, int y).
- Creates JButton instances with text "Top", "Bottom", "Left" and "Right" and
assigns them to the corresponding instance variables.
- Gets the content pane of the frame.
- Adds the four buttons to the content pane, in the positions
North, South,
West and East.
Instructions
- Select File|New Project Next> and create a
new Java application called MyEx6_1. In the
New Java Application wizard name the project MyEx6_1
and set the Project Location to
C:\M257\Unit 6. Change
the name of the Main Class to myex6_1.FrameDemo1Test.

- Add a new Java class called FrameDemo1
to the project in the same package. Write the code for this class so
that it meets the specification above.
- Now complete the main class
FrameDemo1Test so it will:
- Create a new instance of FrameDemo1
with the title "A4 Page";
- Make the new frame visible.
- Run the project to test your code.
- Our sample solution appears in the Solution file.