Java Reference
In-Depth Information
What FlowLayout does to components in a container is best understood by
an example. The following FlowLayoutDemo program creates a Frame and
assigns it FlowLayout. Components are then added using the add() method.
Study the program and see if you can determine its output, which is shown in
Figure 12.4.
import java.awt.*;
public class FlowLayoutDemo
{
public static void main(String [] args)
{
Frame f = new Frame(“FlowLayout demo”);
f.setLayout(new FlowLayout());
f.add(new Button(“Red”));
f.add(new Button(“Blue”));
f.add(new Button(“White”));
List list = new List();
for(int i = 0; i < args.length; i++)
{
list.add(args[i]);
}
f.add(list);
f.add(new Checkbox(“Pick me”, true));
f.add(new Label(“Enter name here:”));
f.add(new TextField(20));
f.pack();
f.setVisible(true);
}
}
The FlowLayoutDemo demonstrates using some of the AWT components.
Three Button components are added to the Frame first. Then, a List is created,
filled with the command-line arguments, and added to the Frame. Next, a
Checkbox, Label, and TextField are added. The pack() method sizes the Frame
so all the components fit nicely, as you can see by the output shown in
Figure 12.4.
Figure 12.4
Output of the FlowLayoutDemo program.
Search WWH ::




Custom Search