Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information

Java Cup Logo

JDC Home Page


Working Applet
Help and Hints
Source Code
Table of Contents
Online Training
shadowSearchFAQFeedback

BorderLayout Help

Help is available for each task, or you can go straight to the BorderLayout source code to see a solution.

Task 1

Create a Panel managed by a BorderLayout.


You need to create a subclass of Applet to hold your BorderLayout. You can call it BorderLayoutTest. In this class, create and instance of BorderLayout and add it to the container. You create a subclass by using the keyword extends. In this case, you want to say:


import java.awt.*;

public class BorderLayoutTest extends java.applet.Applet {

    public void init() {
        setLayout(new BorderLayout());
    }
}

Note the import statement because you are going to be using a number of classes from the AWT later on.

Task 2

Add a Button in each of the five layout positions.


First, create each Button using the new operator. To add each Button to the Applet you must use the add() method and specify one of the five positions allowable for a BorderLayout: "North," "South," "East," "West," or "Center."


    public void init() {
        setLayout(new BorderLayout());

        Button button1 = new Button("North");
        add("North", button1);

        Button button2 = new Button("South");
        add("South", button2);

        Button button3 = new Button("East");
        add("East", button3);

        Button button4 = new Button("West");
        add("West", button4);

        Button button5 = new Button("Center");
        add("Center", button5);
    }