2. Introduction to GUI
• Graphical user interface is a type of user interface
that allows users to interact with the screen using
graphical components(Visual Indicators) rather
than text commands.
• There are two sets of Java APIs(Application
Programming Interface) for graphics
Programming:
1.AWT(Abstract Windowing Toolkit)
2.Swing
3. AWT
• It consist of 12 packages
• Only 2 packages-java.awt and java.awt.event
are commonly used
• The java.awt package contains the core AWT
graphics classes
• GUI component classes(such as Button, Textfield ,label)
• GUI container classes(frame ,panel,Dialog)
• Layout mananger(flowlayout,Borderlayout,Gridlayout)
• Custom graphic classes(Graphic,Color,Font)
4. AWT Events
• The java .awt.event package supports event
handling
• Event classes (ActionEvent,
MouseEvent,KeyEvent and WindowEvent)
• Event Listener Interfaces(Such as
ActionListener,Mouselistener,
WindowsListener)
5. Container
• A frame is the top –level container of an AWT
GUI program
• A panel is a rectangular area(or portion) used
to group related GUI components
• In a GUI program , a component must be kept
in a container
• Every container has a method called
add(Component c)
6.
7. APPLET
• An applet is a java program that runs in a Web browser
• Applet is a container class like Frame
• An applet is a java class that extends the
java.applet.Applet class
• Applets are designed to be embedded within an HTML
page
• When a user views an Html page that contains an
applet ,the code for the applet is downloaded to the
user’s machine
• A JVM is required to view an applet
8. PRATICAL
• How to make an applet Program?
• How a programmer can compile and run an
applet code?
• How applet can be embedded in the HTML
code?
9. LIFE CYCLE OF APPLET
• JVM on the user’s machine creates an instance of
the applet class and invokes various methods
during the applet’s life time
• Four method in the Applet class give you the
framework on which you can build any applet
Application
• Init()
• Start()
• Stop()
• Destroy()
• Paint()(important function but not a part of Lifecycle)
10. How to set Components in Applet
• Component
• Button
• Textfield
• Label
• CheckBox
• List
• We need to design GUI so that user can
interact with the applet
• We have to place component on the applet
container , to meet this requirement
11. For pratical
• Basic Skeleton of an applet
• Creating Component reference variables
• Initialize Components
• Adding Components to the applet
• Setting layout
12. Event Handling in an Applet In java
• Event
Changing the state of an object is knows
as Event. For example click on button,
dragging mouse, minimizing window etc
The java.awt.event package provides many
event classes and Listener interfaces for event
handling
Event handling is to make java code ready to
respond any particular event
13. Feeling of a button
Hey , I am button
class object. user can
click me, but I don’t
know what to run
when he does that
14. Feeling of the programmer(you)
I am the programmer , I have to write code that
should be executed on click of a button
Public void someFunction()
{
write some code
}
Struggling phase:
But do my function get to know that
button has just clicked?
15. Two Ends Of Story
Hey programmer , I have method to
register to register your code with me
Ok that means
button.addActionListener();
16. Communication can solve the issue
Yes , but you have to pass an object to
function
Button.addActionListener(here?);
But object of which class?
17. See prototype defined in Button class
Public void addActionListener(ActionListener,
actionlistener)
But ActionListener ia an interface, which I have to
pass an object of class which implements
ActionLIstener.
Button.addActionListener(object);
18. Ok, then I have to make a class which implements
ActionListener
Class Myhandler implements ActionListener
{
Public void someFunction (ActionEvent e)
{
write some code
}
}
20. Now I have an object
of Myhandler
referenced by
actionListener(a
reference variable of
ActionListener)
21. Parameter Passing in an Applet
<param> tag
• Parameter are passed to applets in Name=Value
pairs in <param> tags betweeen the opening and
closing applet tags
• Inside the applet , you read the values passed
through the param tags with the getParameter()
method of the java.applet.Applet class
25. Frame
• The Frame is the container that contain title bar
and can have menu bars. It can have other
components like button, textfield etc.
• The window is the container that have no borders
and menu bars. You must use frame, dialog or
another window for creating a window.
• The Panel is the container that doesn't contain
title bar and menu bars. It can have other
components like button, textfield etc.
26. Working with Frame Window
• Setting the window’s Dimensions
Void setSize(init newWidth, init newHeight)
void setSize(Dimension newSize)
getSize() method is used to obtain current size
of a window.
• Hiding and showing a window
void setVisible(boolean visibleFlag)
Setting a window’s Title
void setTitle(String newTitle)
27. Example of AWT frame
Import java.awt.*;
Class MyFrame extends Frame
{
MyFrame()
{
setTitle(“Myfirst Frame”);
setSize(200,200);
Button b=new Button(“click Me”);
setLayout(null);
b.setBounds(50,50,50,50);
add(b);
setVisible(true);
}
}