Saturday 25 March 2017

AWT Classes, Window Fundamentals & Working with Frame Windows - Java Tutorials

AWT Classes

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 top-down, hierarchical fashion, it is easier to understand and use than you might at first believe. lists some of the many AWT classes.


Some AWT Classes

AWTEvent:  Encapsulates AWT events.

AWTEventMulticaster:  Dispatches events to multiple listeners.

BorderLayout:  The border layout manager. Border layouts use five components: North, South, East, West, and Center.

Button:  Creates a push button control.

Canvas:  A blank, semantics-free window.

CardLayout:  The card layout manager. Card layouts emulate index cards. Only the one on top is showing.

Checkbox:  Creates a check box control.

CheckboxGroup:  Creates a group of check box controls.

CheckboxMenuItem:  Creates an on/off menu item.

Choice:  Creates a pop-up list.

Color:  Manages colors in a portable, platform-independent fashion.

Component:  An abstract superclass for various AWT components.

Container:  A subclass of Component that can hold other components.

Cursor:  Encapsulates a bitmapped cursor.

Dialog:  Creates a dialog window.

Dimension:  Specifies the dimensions of an object. The width is stored in width, and the height is stored in height.

Event:  Encapsulates events.

EventQueue:  Queues events.

FileDialog:  Creates a window from which a file can be selected.

FlowLayout:  The flow layout manager. Flow layout positions components left to right, top to bottom.

Font:  Encapsulates a type font.

FontMetrics Encapsulates various information related to a font. This information helps you display text in a window.

Frame:  Creates a standard window that has a title bar, resize corners, and a menu bar.

Graphics:  Encapsulates the graphics context. This context is used by the various output methods to display output in a window.

GraphicsDevice:  Describes a graphics device such as a screen or printer.

GraphicsEnvironment:  Describes the collection of available Font and GraphicsDevice objects.

GridBagConstraints:  Defines various constraints relating to the GridBagLayout class.

GridBagLayout:  The grid bag layout manager. Grid bag layout displays components subject to the constraints specified by GridBagConstraints.

GridLayout:  The grid layout manager. Grid layout displays components in a two-dimensional grid.

Image:  Encapsulates graphical images.

Insets:  Encapsulates the borders of a container.

Label:  Creates a label that displays a string.

List:  Creates a list from which the user can choose. Similar to the standard Windows list box.

MediaTracker:  Manages media objects.

Menu:  Creates a pull-down menu.

MenuBar:  Creates a menu bar.

MenuComponent:  An abstract class implemented by various menu classes.

MenuItem:  Creates a menu item.

MenuShortcut Encapsulates a keyboard shortcut for a menu item.

Panel:  The simplest concrete subclass of Container.

Point:  Encapsulates a Cartesian coordinate pair, stored in x and y.

Polygon:  Encapsulates a polygon.

PopupMenu:  Encapsulates a pop-up menu.

PrintJob:  An abstract class that represents a print job.

Rectangle:  Encapsulates a rectangle.

Robot:  Supports automated testing of AWT- based applications. (Added by Java 2, vl.3)

Scrollbar:  Creates a scroll bar control.

ScrollPane:  A container that provides horizontal and/or vertical scroll bars for another component.

SystemColor:  Contains the colors of GUI widgets such as windows, scroll bars, text, and others.

TextArea:  Creates a multiline edit control.

TextComponent:  A superclass for TextArea and TextField.

TextField:  Creates a single-line edit control.

Toolkit:  Abstract class implemented by the AWT.

Window:  Creates a window with no frame, no menu bar, and no title.


Although the basic structure of the AWT has been the same since Java 1.0, some of the original methods were deprecated and replaced by new ones when Java 1.1 was released. For backward-compatibility, Java 2 still supports all the original 1.0 methods. However, because these methods are not for use with new code, this book does not describe them.




Window Fundamentals

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.


Component

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.


Container

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.


Panel

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 superclass 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 you don’t see these items when an applet is run inside a browser. When you 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, you can position and resize them manually using the setLocation( ), setSize( ), or setBounds( ) methods defined by Component.


Window

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, you won’t create Window objects directly. Instead, you will use a subclass of Window called Frame, described next.


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 you 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. (An applet that could masquerade as a host-based application could be used to obtain passwords and other sensitive information without the user’s knowledge.) When a Frame window is created by a program rather than an applet, a normal window is created.


Canvas

Although it is not part of the hierarchy for applet or frame windows, there is one other type of window that you will find valuable: Canvas. Canvas encapsulates a blank window upon which you can draw. You will see an example of Canvas later in this book.




Working with Frame Windows

After the applet, the type of window you will most often create is derived from Frame. You will use it to create child windows within applets, and top-level or child windows for applications. As mentioned, it creates a standard-style window.

Here are two of Frame’s constructors:

      Frame( )
      Frame(String title)

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. Notice that you cannot specify the dimensions of the window. Instead, you must set the size of the window after it has been created. There are several methods you will use when working with Frame windows. They are examined here.


Setting the Window’s Dimensions

The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:

      void setSize(int newWidth, int newHeight)
      void setSize(Dimension newSize)

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 shown here:

      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 you call setVisible( ). Its signature is shown here:

      void setVisible(boolean visibleFlag)

The component is visible if the argument to this method is true. Otherwise, it is hidden.


Setting a Window’s Title

You 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, your program must remove that window from the screen when it is closed, by calling setVisible(false). To intercept a window-close event, you must implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), you must remove the window from the screen. The example in the next section illustrates this technique.

1 comment: