More Related Content
Similar to Dacj 2-2 a (20)
Dacj 2-2 a
- 1. Applets and Swings
Pre-assessment Questions
• The two subclasses of the Throwable class are:
• Exception class and Error class
• Exception class and Object class
• Error class and RunTimeException class
• Exception class and RunTimeException class
• The Throwable class is the sub class of _______ class.
a. Exception
b. Error
c. Object
d. RunTimeException
©NIIT Programming in Java Lesson 2A / Slide 1 of 47
- 2. Applets and Swings
Pre-assessment Questions (Contd.)
1. Which exception occurs when you try to create an object of an abstract class
or interface?
a. ClassNotFoundException
b. IllegalAccessException
c. InstantiationException
d. NoSuchMethodException
3. You use _________ as a command-line option to make the Java compiler
accept the code containing assertions.
a. –source 1.2
b. –source 1.4
c. –source 1.1.2
d. –source 1.1.4
©NIIT Programming in Java Lesson 2A / Slide 2 of 47
- 3. Applets and Swings
Pre-assessment Questions (Contd.)
• Consider the statements:
Statement A: The scope of the catch block is restricted to the statements in
the preceding try block only.
Statement B: A try block must have at least one catch block that follows it
immediately.
Which of the following options is true?
• Statement A is true and statement B is false
• Statement A is false and statement B is true
• Both, statements A and B, are true
• Both, statements A and B, are false
©NIIT Programming in Java Lesson 2A / Slide 3 of 47
- 4. Applets and Swings
Solutions to Pre-assessment
Questions
• a. Exception class and Error class
• c. Object
• c. InstantiationException
• b. –source 1.4
• c. Both, statements A and B, are true
©NIIT Programming in Java Lesson 2A / Slide 4 of 47
- 5. Applets and Swings
Objectives
In this lesson, you will learn to:
• Create applets in Java
• Identify various stages of an applet life cycle
• Identify various graphic methods in Java
• Use layout managers
• Create Abstract Windowing Toolkit control components
• Create Swing components
• Create a Java applet
©NIIT Programming in Java Lesson 2A / Slide 5 of 47
- 6. Applets and Swings
Creating Applets in Java
• AWT Package
• JDK consists of a package called Abstract Window Toolkit (AWT).
• AWT is an Application Programming Interface (API) that is responsible for
building the Graphical User Interface (GUI) in Java.
• The API of the AWT package consists of a collection of classes and
methods that enables you to design and manage the Graphical User
Interface (GUI) applications.
• The AWT package supports applets, which help in creating containers,
such as frames or panels that run in the GUI environment.
• Introduction to Applets
• An applet is a Java program that can be embedded in an HTML Web page.
• An applet is compiled on one computer and can run on another computer
through a Java enabled Web browser or an appletviewer.
• Applets are developed to support the GUI in Java.
©NIIT Programming in Java Lesson 2A / Slide 6 of 47
- 7. Applets and Swings
Creating Applets in Java (Contd.)
• The Applet Class
• The Applet class is a member of the Java API package, java.applet.
• You use the Applet class to create a Java program that displays an applet.
The following figure shows the hierarchical representation of the Java
classes:
©NIIT Programming in Java Lesson 2A / Slide 7 of 47
- 8. Applets and Swings
Creating Applets in Java (Contd.)
• The Applet Class (Contd.)
• The Applet class contains various methods that are used to display text
and image, play an audio file, and respond when you interact with an
applet.
• The following table lists the various methods of the Applet class:
Method Function
void init() Begins the execution of an applet
when the Web browser or Java tool
calls it.
void start() Starts the execution of an applet
when the Web browser or Java tool
calls it.
void update() Calls the paint() method to redraw the
drawing area.
©NIIT Programming in Java Lesson 2A / Slide 8 of 47
- 9. Applets and Swings
Creating Applets in Java (Contd.)
• The Applet Class (Contd.)
• The following table lists the various methods of the Applet class (Contd.):
Method Function
void paint() Redraws the applet’s output.
void destroy() Removes the applet completely from the
memory.
void stop() Suspends the execution of an applet
when the Web browser or Java tool calls
it. The execution of the applet can be
resumed by using the start() method.
©NIIT Programming in Java Lesson 2A / Slide 9 of 47
- 10. Applets and Swings
Creating Applets in Java (Contd.)
• To create an applet, you need to follow these steps:
1. Create a Java program for the Applet.
2. Compile the Java program.
3. Create a Web page that contains an applet.
4. Run the applet.
• You run an applet in an appletviewer by giving the following command on
the command prompt:
appletviewer filename.html
• You can also run an applet in a Web browser.
©NIIT Programming in Java Lesson 2A / Slide 10 of 47
- 11. Applets and Swings
Demonstration-Creating an Applet
• Problem Statement
• MoneySaver Corporation bank has opened branches worldwide
and need to develop an application that can be accessed
across the globe. The bank has also decided to provide online
services to its customers by developing a Website. Steve, the
programmer in the bank is assigned the task of creating the
home page of the Website. Help Steve to develop the proposed
application.
©NIIT Programming in Java Lesson 2A / Slide 11 of 47
- 12. Applets and Swings
Demonstration-Creating an Applet
(Contd.)
• Solution
• Steve uses Applets to design the home page of the Website. The
HomeApplet class is created that displays information about the
various services provided by the bank. To solve the given
problem, perform the following tasks:
1. Code the application.
2. Compile and execute the application.
©NIIT Programming in Java Lesson 2A / Slide 12 of 47
- 13. Applets and Swings
Using HTML Tags in Applets
• You need to include a java class file in an HTML file to load that file in a Web
browser.
• The APPLET tag is used to embed an applet in an HTML document.
• The APPLET tag is written within the BODY tag of the HTML document.
• The following syntax shows how to specify different parameters in an APPLET
tag:
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
©NIIT Programming in Java Lesson 2A / Slide 13 of 47
- 14. Applets and Swings
Using HTML Tags in Applets
(Contd.)
• The following syntax shows how to specify different parameters in an APPLET
tag: (Contd.)
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME= parameter_name VALUE=value of the
parameter>
</APPLET>
</BODY>
</HTML>
©NIIT Programming in Java Lesson 2A / Slide 14 of 47
- 15. Applets and Swings
Using HTML Tags in Applets
(Contd.)
• The various attributes of an Applet tag are:
• CODE and CODEBASE
• The CODE attribute is used to indicate the name of the class file
that holds the current Java applet.
• The CODE attribute is used when both the .java file and the .html
file are located in the same directory.
• The CODEBASE attribute indicates the pathname where the .class
file is stored.
• The CODEBASE attribute is used, if you store a java file in a
directory different from an HTML file.
• The CODE attribute specifies the name of the class file whereas the
CODEBASE attribute contains an alternate pathname where the
classes are stored.
©NIIT Programming in Java Lesson 2A / Slide 15 of 47
- 16. Applets and Swings
Using HTML Tags in Applets
(Contd.)
• The various attributes of an Applet tag are: (Contd.)
• HSPACE and VSPACE
• The HSPACE and VSPACE attributes specify the horizontal and
vertical spaces between an applet and the text.
• The HSPACE attribute controls the space to the left and right side of
an applet.
• The VSPACE attribute controls the space above and below an
applet.
• ALIGN
• The ALIGN attribute specifies the alignment of an applet.
• You can specify various values to the ALIGN attribute, such as
LEFT, RIGHT, BOTTOM, TOP, BASELINE, MIDDLE, TEXTTOP,
ABSBOTTOM, and ABSMIDDLE.
©NIIT Programming in Java Lesson 2A / Slide 16 of 47
- 17. Applets and Swings
Using HTML Tags in Applets
(Contd.)
• The ALT attribute specifies the alternate text to be displayed if the
browser does not support the applet.
• Passing Parameters to Applets
• You need to pass parameters to an applet whenever you want to
send the details to a Java file through an HTML file.
• You pass parameters to an applet by using the <PARAM> tag. The
PARAM tag contains the NAME and VALUE attributes.
• The NAME attribute specifies the name of the parameter passed to
an applet, and the VALUE attribute specifies the value of the
variable to be passed.
• The following syntax sets the value of the color as red:
<APPLET CODE=”MyFirstApplet.class” HEIGHT = 20 WIDTH =
20>
<PARAM NAME= “color” VALUE= “Red”>
</APPLET>
©NIIT Programming in Java Lesson 2A / Slide 17 of 47
- 18. Applets and Swings
Using HTML Tags in Applets
(Contd.)
• Getting Parameters in Applets
• You retrieve the parameters passed to an applet by using the
getParameter() method in the Java file.
• This method accepts a String argument that indicates the name of the
parameter.
• The getParameter() method returns a String value that specifies the
corresponding value of that parameter.
©NIIT Programming in Java Lesson 2A / Slide 18 of 47
- 19. Applets and Swings
Identifying the Various Stages of an
Applet Life Cycle
• The life cycle of an applet describes the sequence of stages, which begin
when an applet is loaded in an appletviewer or a Web browser and ends
when the applet is destroyed.
• An applet inherits the properties and methods of the Applet class.
• Java provides init(), start(), stop(), paint(), and destroy() as the
basic applet methods to control the execution of an applet.
• The different stages of an applet life cycle are:
• Initializing an applet
• Starting the applet
• Stopping the applet
• Destroying the applet
©NIIT Programming in Java Lesson 2A / Slide 19 of 47
- 20. Applets and Swings
Identifying the Various Stages of an
Applet Life Cycle (Contd.)
• The different stages of an applet life cycle are: (Contd.)
• Initializing an Applet
• The init() method initializes an applet when the applet is loaded
for the first time.
• It defines the objects and variables that are required to execute an
applet.
• You apply the settings for fonts, colors, and initial parameters,
such as variables and constants in the init() method.
• The init() method is also used to add components, such as
buttons and check boxes to an applet.
• The following syntax shows how to define the init() method:
public void init()
{ }
©NIIT Programming in Java Lesson 2A / Slide 20 of 47
- 21. Applets and Swings
Identifying the Various Stages of an
Applet Life Cycle (Contd.)
• The different stages of an applet life cycle are: (Contd.)
• Starting the Applet
• The start() method is called to start the execution of an applet
after it’s initialized with the init() method.
• The start() method can be called more than once in an applet.
• The following syntax shows how to define the start() method:
public void start()
{
/* start() method definitions. */
}
©NIIT Programming in Java Lesson 2A / Slide 21 of 47
- 22. Applets and Swings
Identifying the Various Stages of an
Applet Life Cycle (Contd.)
• The different stages of an applet life cycle are: (Contd.)
• Stopping the Applet
• The stop() method suspends the execution of an applet.
• The stop() method is called when either an end user stops an
applet or an applet loses the focus.
• You can use the stop() method to reset the variables and stop a
running applet.
• The following syntax shows how to define the stop() method:
public void stop()
{
/* stop() method definitions. */
}
©NIIT Programming in Java Lesson 2A / Slide 22 of 47
- 23. Applets and Swings
Identifying the Various Stages of an
Applet Life Cycle (Contd.)
• The different stages of an applet life cycle are: (Contd.)
• Destroying the Applet
• The destroy() method is called when an applet is destroyed.
• When you want to exit from the Web browser or appletviewer of
Java, an applet calls this method to release the resources, such as
parameters and images.
• This method occurs only once in the life cycle of an applet.
• The following syntax shows how to define the destroy() method:
public void destroy()
{
/* destroy() method definitions. */
}
©NIIT Programming in Java Lesson 2A / Slide 23 of 47
- 24. Applets and Swings
Identifying the Various Stages of an
Applet Life Cycle (Contd.)
• The following figure shows the life cycle of an applet:
©NIIT Programming in Java Lesson 2A / Slide 24 of 47
- 25. Applets and Swings
Various Graphic Methods in
Java
• AWT supports various graphics methods that enable you to draw shapes,
such as line, arc, ellipse, circle, and rectangle in an applet.
• Drawing Lines, Rectangles, and Polygons
• You need to draw lines, rectangles, and polygons in an applet for
creating drawings in an applet.
• The drawLine() method is used to draw lines in an applet.
• The following syntax shows how to define the drawLine() method:
void drawLine(int x1, int y1, int x2, int y2)
In the preceding syntax, the x1 and y1 arguments are the starting
coordinates of a line. The x2 and y2 arguments are the ending
coordinates of the line.
©NIIT Programming in Java Lesson 2A / Slide 25 of 47
- 26. Applets and Swings
Various Graphic Methods in
Java (Contd.)
• Drawing Lines, Rectangles, and Polygons (Contd.)
• The drawRect() is used to draw a rectangle.
• The following syntax shows how to define the drawRect() method:
void drawRect(int x, int y, int width, int length)
In the preceding syntax, the x and y arguments specify the top left
coordinates of a rectangle. The width and length arguments specify the
dimensions of the rectangle.
• You can also draw arbitrary shapes, such as polygons using the
drawPolygon() and fillPolygon() methods.
• The following syntax shows how to use the drawPolygon() method:
(int x[], int y[], int num)
In the preceding syntax, the x and y arguments specify an array of
integers representing the x and y coordinates of a polygon. The num
argument specifies the total number of points of the polygon.
©NIIT Programming in Java Lesson 2A / Slide 26 of 47
- 27. Applets and Swings
Various Graphic Methods in
Java (Contd.)
• Drawing Arcs, Circles, and Ellipses
• You can draw an arc using the drawArc() method.
• The following syntax shows how to define the drawArc() method:
void drawArc(int x, int y, int width, int height, int
startAngle, int sweepAngle)
In the preceding syntax, the x and y arguments represent the top left
end coordinates of a bounding rectangle. The width and height
arguments represent the dimensions of the rectangle. The startAngle
argument represents the angle at which an arc starts and the
sweepAngle argument represents the angular distance covered by the
arc.
• The drawOval() method is used for drawing circles and ellipses.
• The following syntax shows how to define the drawOval() method:
void drawOval(int x, int y, int width, int height)
In the preceding syntax, the x and y arguments are the top left
endpoints of a rectangle enclosing a circle or an ellipse.
©NIIT Programming in Java Lesson 2A / Slide 27 of 47
- 28. Applets and Swings
Various Graphic Methods in
Java (Contd.)
• Painting Various Graphic Objects
• The fillPolygon() method is used to draw a filled polygon in an
applet.
• The following syntax shows how to define the fillPolygon() method:
void fillPolygon(int x[], int y[], int num)
In the preceding syntax, the x and y arguments specify an array of
integers representing the x and y coordinates of a polygon. The num
argument specifies the total number of points of the polygon.
• The fillOval()method is used to draw filled circles and ellipses.
• The following syntax shows how to define the fillOval() method:
void fillOval(int x, int y, int width, int height)
In the preceding syntax, the x and y arguments are the top left
endpoints of an oval.
©NIIT Programming in Java Lesson 2A / Slide 28 of 47
- 29. Applets and Swings
Various Graphic Methods in
Java (Contd.)
• Painting Various Graphic Objects (Contd.)
• You can draw filled arcs using the fillArc() method.
• The following syntax shows how to define the fillArc() method:
void fillArc(int x, int y, int width, int height, int
startAngle, int sweepAngle)
In the preceding syntax, the x and y arguments represent the top left
end coordinates of a rectangle. The width and height arguments
represent the dimensions of the rectangle. The startAngle argument
represents the angle at which an arc starts and the sweepAngle
argument represents the angular distance covered by the arc.
©NIIT Programming in Java Lesson 2A / Slide 29 of 47
- 30. Applets and Swings
Various Graphic Methods in
Java (Contd.)
• Painting Various Graphic Objects (Contd.)
• You can also draw a filled rectangle in an applet using the fillRect()
method. The following syntax shows how to define the fillRect()
method:
void fillrect(int x, int y, int width, int length)
In the preceding syntax, the x and y arguments specify the top left
corner coordinates of a rectangle. The width and length arguments
specify the dimensions of the rectangle.
©NIIT Programming in Java Lesson 2A / Slide 30 of 47
- 31. Applets and Swings
Demonstration-Using Various Graphic
Methods in Java
• Problem Statement
• Certified Carriers is an institute that conducts various computer
courses. John, the instructor at the institute teaches Java
language to students. After the completion of the Java course,
John gives a home assignment to the students to create a Java
program that draws a figure using various shapes taught in the
lecture. Help students to create the application.
©NIIT Programming in Java Lesson 2A / Slide 31 of 47
- 32. Applets and Swings
Demonstration-Using Various Graphic
Methods in Java (Contd.)
• Solution
• Students will use Applets to create a program that displays
various shapes in an applet. The Smiley class is created that
draws a smiley in an applet. To solve the given problem, perform
the following tasks:
• Code the application.
• Compile and execute the application.
©NIIT Programming in Java Lesson 2A / Slide 32 of 47
- 33. Applets and Swings
The AWT Control Components
• Java provides the AWT control components, which contains classes that
enable you to create standard components, such as buttons, labels, and text
fields in Java.
• A Java component enables you to accept input from an end user.
• You can position AWT components in containers using the different layout
managers.
• Using Various AWT Components
• An AWT control is a component that enables end users to interact with
applications created in Java.
• All AWT controls in Java are subclasses of the Component class.
• The Component class provides the add() method to add AWT
components to containers, such as an applet or a window.
• TextField: User interface components that accept text input from
an end user. A text field enables you to type text in a single line.
An instance of the TextField class is used to create a text field.
©NIIT Programming in Java Lesson 2A / Slide 33 of 47
- 34. Applets and Swings
The AWT Control Components(Contd.)
• Using Various AWT Components (Contd.)
• TextArea: Used to accept text input from an end user, but it
enables you to type text in multiple lines. An instance of the
TextArea class is used to create a text area.
• Button: Used for handling events. Java provides the Button class
to create AWT button components.
• List: Is a scrollable list of text items that enables you to select
either one item or multiple items. You create a list using the List
class in Java.
• CheckBox: Exist in dual state, checked and unchecked. You can
change the state of a check box by clicking the check box. You
create a check box using the CheckBox class.
• Choice: Used to create combination boxes that are also known as
drop-down menus using the Choice class in Java. You create a
drop-down menu using the Choice class that enables you to select
a single item from the menu.
• Labels: Used for displaying a single line of text in a container. You
create a label using the Label class.
©NIIT Programming in Java Lesson 2A / Slide 34 of 47
- 35. Applets and Swings
The Swing Components
• Swing components are a collection of lightweight visual components that
provide a replacement for the heavyweight AWT components.
• Swing components contain the Pluggable Look and Feel (PL&F) feature that
allows applications to have the same behavior on various platforms.
• Identifying the Swing Component Class Hierarchy
• The JComponent class is the root of the Swing hierarchy, which is an
extension of the AWT container class.
• The class hierarchy of the Swing components is categorized into:
• Top-level Swing Containers: Acts as a container for placing the
intermediate-level and atomic swing components, such as panels,
frames, buttons, and check boxes.
• Intermediate-level Swing Containers: Placed on the top-level
containers and contains atomic components.
• Atomic Components: Placed on the intermediate-level swing
containers. Atomic components are used to accept input from a
user.
©NIIT Programming in Java Lesson 2A / Slide 35 of 47
- 36. Applets and Swings
The Swing Components (Contd.)
• Using the Top-level Swing Containers
• JApplet
• The JApplet class is an extension of the AWT applet class.
• The Swing components that contain an applet need to extend the
JApplet class.
• The JApplet() constructor enables you to create a swing applet
instance when you create an instance of the JApplet class.
• JFrame
• The JFrame class is an extension of the AWT Frame class.
• You cannot add components directly to JFrame.
©NIIT Programming in Java Lesson 2A / Slide 36 of 47
- 37. Applets and Swings
The Swing Components (Contd.)
• Using the Top-level Swing Containers (Contd.)
• JDialog
• The JDialog class is an extension of the AWT java.awt.Dialog class.
• The JDialog class is used to create modal and non-modal dialog
boxes.
• Using the Intermediate Level Swing Containers
• JPanel
• JPanel class is an extension of the JComponent class that provides
a replacement for the AWT Panel class.
• You create a panel and add various components to it.
• The panel is further added to the content pane, which represents
the display area of a window, a dialog, or a frame.
• JPanel supports all layout managers of AWT.
• By default, JPanel applies the flow layout manager.
©NIIT Programming in Java Lesson 2A / Slide 37 of 47
- 38. Applets and Swings
The Swing Components (Contd.)
• Using the Intermediate Level Swing Containers (Contd.)
• JTabbedPane
• The JTabbedPane class is used to create a tabbed pane
component that enables you to switch between groups of
components by clicking a tab with a given label.
• Tabs are added to the JTabbedPane object by using the
addTab() method.
• The JTabbedPane class enables you to add multiple
components but it displays only a single component at a time.
• Using the Atomic Components
• JButton
• JTextField
• JCheckBox
• JComboBox
• JLabel
• JRadioButton
©NIIT Programming in Java Lesson 2A / Slide 38 of 47
- 39. Applets and Swings
Using Layout Managers
• The layout managers are used to position the components, such as an
applet, a panel, or a frame in a container.
• The layout managers implement the java.awt.LayoutManager interface.
• A layout manager is an instance of the LayoutManager interface in Java.
• You can use the following method to apply the desired layout to the
components:
void setLayout(layoutManager obj)
In the preceding syntax, obj is the reference to the desired layout manager.
• Java has various predefined classes of layout managers.
• All layout managers make use of the setLayout() method to set the layout
of a container.
• If the setLayout() method is not used, then the default layout of the
container is set.
©NIIT Programming in Java Lesson 2A / Slide 39 of 47
- 40. Applets and Swings
Using Layout Managers (Contd.)
• The different types of layout managers are:
• FlowLayout Manager
• The flow layout is the default layout manager used for the Applet
class.
• In the flow layout manager, the components are placed in a
container window in a sequence one after the other in rows.
• Java provides the FlowLayout class to apply flow layout to the
various components that you are inserting in an applet.
• You can use the following constructors to create an instance of the
FlowLayout class:
• FlowLayout()
• FlowLayout(int align)
• FlowLayout(int align, int hgap,int vgap)
©NIIT Programming in Java Lesson 2A / Slide 40 of 47
- 41. Applets and Swings
Using Layout Managers (Contd.)
• The different types of layout managers are:
• BorderLayout Manager
• BorderLayout is the default layout of the Frame class.
• The BorderLayout layout manager divides the container into
north, south, east, west, and centre regions.
• You can place five components or controls in each part.
• Java provides the BorderLayout class to apply the border
layout to the components.
• The setLayout() method is used for applying border layout
to a container.
• You specify the directions for the BorderLayout using the
BorderLayout.NORTH, BorderLayout.SOUTH,
BorderLayout.EAST, BorderLayout.WEST, and
BorderLayout.CENTER constants.
• You can use the following constructors to create an instance
of the BorderLayout class:
BorderLayout()
BorderLayout(int h, int v)
©NIIT Programming in Java Lesson 2A / Slide 41 of 47
- 42. Applets and Swings
Using Layout Managers (Contd.)
• The different types of layout managers are: (Contd.)
• GridLayout Manager
• The grid layout is the layout that divides the container into rows
and columns.
• The intersection of a row and a column of the grid layout is called
cell.
• The GridLayout class of Java enables you to create a grid layout.
• All the components in a grid are of the same size.
• You can use the following constructors to create an instance of the
GridLayout class:
• GridLayout()
• GridLayout(int r, int c)
• GridLayout(int r, int c, int h, int v)
©NIIT Programming in Java Lesson 2A / Slide 42 of 47
- 43. Applets and Swings
Using Layout Managers (Contd.)
• The different types of layout managers are: (Contd.)
• CardLayout Manager
• The CardLayout class is used to implement an area that contains
different components at different times.
• The CardLayout is often controlled by a combo box, and the state
of the combo box determines which panel (group of components)
CardLayout displays.
• You can use the following constructors to create an instance of the
CardLayout class:
• CardLayout()
• CardLayout(int hgap, int vgap)
©NIIT Programming in Java Lesson 2A / Slide 43 of 47
- 44. Applets and Swings
Using Layout Managers (Contd.)
• The different types of layout managers are: (Contd.)
• GridBagLayout Manager
• GridBagLayout places components in a grid of rows and columns,
allowing specified components to span multiple rows or columns.
Not all rows necessarily have the same height. Similarly, not all
columns necessarily have the same width.
• GridBagLayout places components in rectangles (cells) in a grid
and uses the components' preferred sizes to determine the size of
the cells.
• You can use the following constructor to create an instance of the
GridBagLayout class:
• GridBagLayout()
©NIIT Programming in Java Lesson 2A / Slide 44 of 47
- 45. Applets and Swings
Demonstration-Using Layout
Managers
• Problem Statement
• MoneySaver Corporation is a bank providing various services to
its customers. The bank has opened various branches in
different countries. The bank has developed a Website that
enables customers to access the bank services online. Steve, the
bank manager wants to add a feedback page in the Website so
that the customers can give their feedback about the bank.
Mary, the programmer in the bank is assigned the task of adding
a feedback page in the Website. Help Mary to develop the
proposed application.
©NIIT Programming in Java Lesson 2A / Slide 45 of 47
- 46. Applets and Swings
Demonstration-Using Layout Managers
(Contd.)
• Solution
• Mary can use Applets and Swings to design the feedback page.
• Using the Applets
The BankApplet class is created that displays various fields
included in the feedback page. To solve the given problem,
perform the following tasks:
1. Code the application.
2. Compile and execute the application.
• Using the Swings
The BankSwing class is created that displays various fields
included in the feedback page. To solve the given problem,
perform the following tasks:
1. Code the application.
2. Compile and execute the application.
©NIIT Programming in Java Lesson 2A / Slide 46 of 47
- 47. Applets and Swings
Summary
In this lesson, you learned:
• An Applet is a Java program that you can embed in a HTML page and access
on a Web browser.
• Java Application Program Interface (API) includes a package java.applet,
which contains the Applet class.
• Applet has limited access to system resources and prohibited access to other
systems on the network.
• All the applets are subclasses of the Applet class. Most classes in Java extend
other classes.
• The life cycle of an applet consists of the following methods:
• init()
• start()
• stop()
• destroy()
• You can run an applet in an appletviewer or a Web browser.
©NIIT Programming in Java Lesson 2A / Slide 47 of 47
- 48. Applets and Swings
Summary (Contd.)
• To run an applet in appletviewer, include an HTML tag as a comment.
Compile the applet using javac command and run the applet giving the
following command:
% appletviewer className.java
• The parameters of an applet HTML tag are:
• Code
• Hspace and Vspace
• <PARAM> tag
• The various drawing methods in Java are:
• drawLine()
• drawRect()
• draw3Drect()
• drawOval()
• drawArc()
• fillOval()
• fillArc()
©NIIT Programming in Java Lesson 2A / Slide 48 of 47
- 49. Applets and Swings
Summary (Contd.)
• The various layout Managers in Java are:
• BorderLayout
• FlowLayout
• GridLayout
• CardLayout
• GridBagLayout
• The various Swing Components are:
• JApplet
• JPanel
• JButton
• JComboBox
• JCheckBox
• JTextField
• JRadioButton
• JLabel
©NIIT Programming in Java Lesson 2A / Slide 49 of 47