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

FlowLayout Help

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

Task 1

Create a Panel managed by a FlowLayout.


You need to create a subclass of Applet to hold your FlowLayout. You can call it FlowLayoutTest. In this class, create an instance of FlowLayout 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 FlowLayoutTest extends java.applet.Applet {

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

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

Task 2

Add five Buttons to the Applet.


You need to first create each Button using the new operator. To add each Button to the Applet you must use the add() method. FlowLayout takes only one argument to the add() method, which is the reference to the component to be added.


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

        Button button1 = new Button("First");
        add(button1);

        Button button2 = new Button("Second");
        add(button2);

        Button button3 = new Button("Third");
        add(button3);

        Button button4 = new Button("Fourth");
        add(button4);

        Button button5 = new Button("Fifth");
        add(button5);
    }