The AWT contains numerous classes and methods that allow us to create and manage windows. Although the main purpose of the AWT is to support applet windows, it can also be used to create stand -alone windows that run in a GUI environment, such as Windows.
The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. Fortunately, because it is logically organized in a topdown, hierarchical fashion, it is easier to understand and use than you might at first believe.
The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a standard window. Much of the functionality of these windows is derived from their parent classes. Thus, a description of the class hierarchies relating to these two classes is fundamental to their understanding. Figure below shows the class hierarchy for Panel and Frame.
Applet class hierarchy
At the top of the AWT hierarchy is the Component class. Component is an abstract class that encapsulates all of the attributes of a visual component. All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.
The Container class is a subclass of Component. It has additional methods that allow other Component objects to be nested within it. Other Container objects can be stored inside of a Container (since they are themselves instances of Component). This makes for a multileveled containment system. A container is responsible for laying out (that is,positioning) any components that it contains. It does this through the use of various layout managers.
The Panel class is a concrete subclass of Container. It doesn’t add any new methods; it simply implements Container. A Panel may be thought of as a recursively nestable, concrete screen component. Panel is the super-class for Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object. In essence, a Panel is a window that does not contain a title bar, menu bar, or border. This is why we don’t see these items when an applet is run inside a browser. When we run an applet using an applet viewer, the applet viewer provides the title and border. Other components can be added to a Panel object by its add( ) method (inherited from Container). Once these components have been added, we can position and resize them manually using the setLocation( ), setSize( ), or setBounds() methods defined by Component.
The Window class creates a top-level window. A top -level window is not contained within any other object; it sits directly on the desktop. Generally, we won’t create Window objects directly. Instead, we will use a subclass of Window called Frame.
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. If we create a Frame object from within an applet, it will contain a warning message, such as “Java Applet Window,” to the user that an applet window has been created. This message warns users that the window they see was started by an applet and not by software running on their computer. When a Frame window is created by a program rather than an applet, a normal window is created.
Canvas encapsulates a blank window upon which we can draw.
This class encapsulates the ‘width’ and ‘height’ of a component (in integer precision) in a single object. The class is associated with certain properties of components. Several methods defined by the Component class and the Layout Manager interface return a Dimension object.
Normally the values of width and height are non -negative integers. The constructors that allow us to create a dimension do not prevent us from setting a negative value for these properties. If the value of width or height is negative, the behavior of some methods defined by other objects is undefined.
Fields of Dimension:
It creates an instance of Dimension with a width of zero and a height of zero.
It creates an instance of Dimension whose width and height are the same as for the specified dimension.
Dimension(int width, int height)
It constructs a Dimension and initializes it to the specified width and specified height.
boolean equals(Object obj)
It checks whether two dimension objects have equal values.
It returns the height of this dimension in double precision.
It gets the size of this Dimension object.
It returns the width of this dimension in double precision.
void setSize(Dimension d)
It sets the size of this Dimension object to the specified size.
void setSize(double width, double height)
It sets the size of this Dimension object to the specified width and height in double precision.
void setSize(int width, int height)
It sets the size of this Dimension object to the specified width and height.
After the applet, the type of window we will most often create is derived from Frame. We will use it to create child windows within applets, and top-level or child windows for applications. It creates a standard -style window. Following are two of Frame’s constructors:
The first form creates a standard window that does not contain a title. The second form creates a window with the title specified by title. Note that we cannot specify the dimensions of the window. Instead, we must set the size of the window after it has been created.
Setting the Windows Dimensions
The setSize( ) method is used to set the dimensions of the window. Its signature is shown below:
The new size of the window is specified by ‘newWidth’ and ‘newHeight’, or by the ‘width’ and ‘height’ fields of the Dimension object passed in ‘newSize’. The dimensions are specified in terms of pixels. The getSize( ) method is used to obtain the current size of a window. Its signature is:
Dimension getSize( )
This method returns the current size of the window contained within the ‘width’ and ‘height’ fields of a Dimension object.
Hiding and showing a Window
After a frame window has been created, it will not be visible until we call setVisible( ). Its signature is:
void set Visible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.
Setting a Windows Title
We can change the title in a frame window using setTitle( ), which has this general form:
void setTitle(String newTitle)
Here, ‘newTitle’ is the new title for the window.
Closing a Frame Window
When using a frame window, our program must remove that window from the screen when it is closed, by calling set Visible(false). To intercept a window close event, we must implement the windowClosing( ) method of the Window Listener interface. Inside window Closing( ), we must remove the window from the screen.
Creating a Frame Window in an Applet
The following applet creates a subclass of Frame called SampleFrame. A window of this subclass is instantiated within the init( ) method of AppletFrame. Note that ‘SampleFrame’ calls Frame’s constructor. This causes a standard frame window to be created with the title passed in title. This example overrides the applet window’s start( ) and stop( ) method s so that they show and hide the child window, respectively. It also causes the child window to be shown when the browser returns to the applet.
Creating a Windowed Program
Although creating applets is the most common use for Java’s AWT, it is possible to create stand -alone AWT-based applications, too. To do this, simply create an instance of the window or windows we need inside main( ).For example, the following program creates a simple frame window.
Displaying information within a Window
In the most general sense, a window is a container for information.
Although we have already output small amounts of text to a window in the preceding examples,
we have not begun to take advantage of a window’s ability to present high -quality text and graphics.
Indeed, much of the power of the AWT comes from its support for these items. Working with Graphics
Adv Java Related Interview Questions
|Java Liferay Interview Questions||Java Persistence API Interview Questions|
|Java Interview Questions||JavaMail API Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Angular 5 Interview Questions||Java Hadoop Developer Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.