Using Abstract Windowing Toolkit (AWT) In Applications - What Holds Components Together…The Container
(Page 5 of 6 )
Containers: Some components can also act as
containers. A window for example, can be a part of another container. At the same time it can be a parent to other components, such as textbox, and a checkbox, it is a container as well. The container class is a specialized subclass of the Component class. A container may contain zero or more components. These components are called siblings, since they have the same parent window. There are three main types of containers-
Window, Applet and Panel. There are two types of windows-
Frame and Dialog. A frame is a rectangular box with a title and resize button. A dialog box is similar to a frame except that it cannot be resized and does not have a menu bar.
The Frame Class: A frame is a powerful feature of AWT. You can create a window for your application using Frame class. A frame has a title bar, resizable border and an optional menu bar. You can add components to the frame using add () method as frames are derived form
java.awt.container. The
Border layout is the default layout of the frame. A frame receives mouse events, keyboard events and focus events.
The constructor of the Frame class receives the title of the frame as parameter. The string or name you specify will be displayed as the title of the frame.
Frame f = new Frame (“Frame Window Demo”);
After the window is created, it can be displayed by calling the setVisible () method and can be sized by calling the setSize () method. The program below displays a frame.
Import java.awt.*;
Public class frame extends Frame
{
Public static void main (String arg[])
{
Frame frm;
frm = new Frame(“My First AWT Container-The Frame”);
frm.setSize(300,400);
frm.setBackground (Color.red);
frm.setVisible(true)
}
}
Save the above program as frame.java, compile using Javac filename.java and execute using Java file name. The output will be a frame window with the title “My Frist AWT Container-The Frame”.
Next: How To Talk To Your Application >>
More Java Articles
More By Gayathri Gokul