The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > Containers  [3 examples]

e571. Creating a Container

A container holds one or more child components. A container has a layout that determines how the child components are arranged within the container. This example creates a frame with a text area in the center and a row of buttons at the bottom. The buttons are placed in a container.
    // Create a container with a flow layout, which arranges its children horizontally
    Panel panel = new Panel();
    
    // A container can also be created with a specific layout
    panel = new Panel(new FlowLayout(FlowLayout.RIGHT));
    
    // Add several buttons to the container
    panel.add(new Button("A"));
    panel.add(new Button("B"));
    panel.add(new Button("C"));
    
    // Create frame with a text area in the center
    Frame frame = new Frame();
    Component comp = new TextArea();
    frame.add(comp, BorderLayout.CENTER);
    
    // Add the container to the bottom of the frame
    frame.add(panel, BorderLayout.SOUTH);
    
    // Show the frame
    int width = 300;
    int height = 300;
    frame.setSize(width, height);
    frame.setVisible(true);

 Related Examples
e572. Getting the Child Components of a Container
e573. Determining When a Component Is Added or Removed from a Container

See also: Colors    Components    Cursors    Drawing    Events    Focus    Frames    GridBagLayout    Images    Shapes    Simulating Events    Text    The Screen   


© 2002 Addison-Wesley.