0% found this document useful (0 votes)
23 views

Applet

Uploaded by

Nikunj Shah3540
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Applet

Uploaded by

Nikunj Shah3540
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Applet

AWT
Applet
Applet
• 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.
• Except for menus, 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.
Applet
• 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.
Applet
• Panel
• The Panel class is a concrete subclass of Container. A Panel may be
thought of as a recursively nestable, concrete screen component.
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( ),
setPreferredSize( ), or setBounds( ) methods defined by Component.
Applet
• 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.
• 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. The precise look of a Frame will differ among
environments.
Applet
• Canvas: Derived from Component, Canvas encapsulates a blank window upon which you
can draw.
• Here are two of Frame’s constructors:
• Frame( ) throws HeadlessException
• Frame(String title) throws HeadlessException
• 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. A HeadlessException is thrown if an
attempt is made to create a Frame instance in an environment that does not
support user interaction.
Applet
• The setSize( ) method is used to set the dimensions of the window. It
is shown here:
• void setSize(int newWidth, int newHeight)
• void setSize(Dimension newSize)
• The getSize( ) method is used to obtain the current size of a window.
One of its forms 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.
Applet
• 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.
Applet
• 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.
Applet
• Closing a Frame Window
• When using a frame window, your program must remove that
window from the screen when it is closed. If it is not the top-level
window of your application, this is done by calling setVisible(false).
• For the main application window, you can simply terminate the
program by calling System.exit( ) as we
• did. To intercept a window-close event, you must implement the
• windowClosing( ) method of the WindowListener interface.
Applet
• The paint( ) method is called each time an AWT-based application’s output
• must be redrawn. This situation can occur for several reasons. For example, the
• program’s window may be overwritten by another window and then uncovered.
• Or the window may be minimized and then restored. paint( ) is also called when
• the window is first displayed. Whatever the cause, whenever the window must
• redraw its output, paint( ) is called. This implies that your program must have
• some way to retain its output so that it can be redisplayed each time paint( )
• executes.
• The paint( ) method is shown here:
• void paint(Graphics context)
Adapter Class
• Java provides a special feature, called an adapter class, that can
simplify the creation of event handlers in certain situations.
• An adapter class provides an empty implementation of all methods in
an event listener interface.
• Adapter classes are useful when you want to receive and process only
some of the events that are handled by a particular event listener
interface. You can define a new class to act as an event listener by
extending one of the adapter classes and implementing only those
events in which you are interested.
Adapter Class
• For example, the MouseMotionAdapter class has two
methods, mouseDragged( ) and mouseMoved( ), which
are the methods defined by the MouseMotionListener
interface. If you were interested in only mouse drag events, then you
could simply extend MouseMotionAdapter and override
mouseDragged( ). The empty implementation of
mouseMoved( ) would handle the mouse motion events for you.
Listener Interfaces Implemented by Adapter Classes
Layout Managers
• The LayoutManagers are used to arrange components in a
particular manner. The Java LayoutManagers facilitates us
to control the positioning and size of the components in GUI
forms. LayoutManager is an interface that is implemented by
all the classes of layout managers. There are the following
classes that represent the layout managers:
1.java.awt.BorderLayout
2.java.awt.FlowLayout
3.java.awt.GridLayout
4.java.awt.CardLayout
Layout Managers
1.java.awt.GridBagLayout
2.javax.swing.BoxLayout
3.javax.swing.GroupLayout
4.javax.swing.ScrollPaneLayout
5.javax.swing.SpringLayout etc
BorderLayout

• The BorderLayout is used to arrange the components in


five regions: north, south, east, west, and center. Each
region (area) may contain one component only. It is the
default layout of a frame or window. The BorderLayout
provides five constants for each region:
1.public static final int NORTH
2.public static final int SOUTH
3.public static final int EAST
4.public static final int WEST
5.public static final int CENTER
BorderLayout
• Constructors of BorderLayout class:
• BorderLayout(): creates a border layout but with no
gaps between the components.
• BorderLayout(int hgap, int vgap): creates a border
layout with the given horizontal and vertical gaps
between the components.
BorderLayout
GridLayout

• The Java GridLayout class is used to arrange the components in a


rectangular grid. One component is displayed in each rectangle.
• Constructors of GridLayout class
1.GridLayout(): creates a grid layout with one column per
component in a row.
2.GridLayout(int rows, int columns): creates a grid layout with
the given rows and columns but no gaps between the
components.
3.GridLayout(int rows, int columns, int hgap, int
vgap): creates a grid layout with the given rows and columns
along with given horizontal and vertical gaps.
GridLayout
GridLayout
GridLayout

You might also like