Unit-4
Even and GUI Programming
Event Handling in Java
Refer Mouse event program from journal.
GUI (Graphical User Interface)
It refers to an interface that allows one to interact with electronic devices like
computers and tablets through graphic elements. It uses icons, menus and
other graphical representations to display information, as opposed to text-
based commands.
Panel Frames:-
The Panel is a simplest container class. It provides space in which an
application can attach any other component. It inherits the Container class.
Frames
A frame, implemented as an instance of the JFrame class, is a window that
has decorations such as a border, a title, and supports button components that
close or iconify the window.
Layout Manager:-
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.
Border Layout:-
A border layout lays out a container, arranging and resizing its components
to fit in five regions: north, south, east, west, and center.
Grid Layout:-
GUI Components
Buttons:-
A button is basically a control component with a label that generates an
event when pushed.
Check Box:-
The Checkbox class is used to create a checkbox. It is used to turn an
option on (true) or off (false).
Combo Box:-
The object of Choice class is used to show popup menu of choices. Choice
selected by user is shown on the top of a menu.
Radio Button:-
The JRadioButton class is used to create a radio button. It is used to choose
one option from multiple options. It is widely used in exam systems or quiz.
Labels:-
A Label object is a component for placing text in a container. A label
displays a single line of read-only text.
Text Field:-
The object of a TextField class is a text component that allows a user to enter
a single line text and edit it.
Lists:-
List in Java provides the facility to maintain the ordered collection. It
contains the index-based methods to insert, update, delete and search the
elements.
Dialog Box:-
Dialog boxes are graphical components that are usually used to display
errors or give some other information to the user.
APPLET AND ITS LIFE CYCLE
Applets:- Applets are the small programs used to develop run in web
browsers.
Example:-
import [Link];
import [Link];
// HelloWorld class extends Applet
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
[Link]("Hello World", 20, 20);
}
}
Applet Life Cycle:-
Exception Handling in Java:-
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors.
Refer Program from Journal
Write a java program to demonstrate keyboard event.
import [Link].*;
import [Link].*;
public class KeyListenerExample extends Frame implements KeyListener {
Label l;
TextArea area;
KeyListenerExample() {
l = new Label();
[Link] (20, 50, 100, 20);
area = new TextArea();
[Link] (20, 80, 300, 300);
[Link](this);
add(l);
add(area);
setSize (400, 400);
setLayout (null);
setVisible (true);
public void keyPressed (KeyEvent e) {
[Link] ("Key Pressed");
public void keyReleased (KeyEvent e) {
[Link] ("Key Released");
}
public void keyTyped (KeyEvent e) {
[Link] ("Key Typed");
public static void main(String[] args) {
new KeyListenerExample();