Chapter 08 - GUI Programming
Chapter 08 - GUI Programming
Chapter 08 - GUI Programming
1
Java GUI Event Handling 2
What is an Event?
Change in the state of an object is known as event i.e. event describes the
change in state of source.
Events are generated as result of user interaction with the graphical user
interface components.
For example, clicking on a button, moving the mouse, entering a character
through key-board, selecting an item from list, scrolling the page are the
activities that causes an event to happen.
Any program that uses GUI (graphical user interface) such as Java
application written for windows, is event driven.
Event-handling model
Three parts
Event source
GUI component with which user interacts
Event object
Encapsulates information about event that occurred
Event listener
Receives event object when notified, then responds
Programmer must perform two tasks
Register event listener for event source
Implement event-handling method (event handler)
Co m p o ne nt De sc rip tio n
JLabel An area where uneditable text or icons can be displayed.
JTextField An area in which the user inputs data from the keyboard.
The area can also display information.
JButton An area that triggers an event when clicked.
JCheckBox A GUI component that is either selected or not selected.
JComboBox A drop-down list of items from which the user can make
a selection by clicking an item in the list or possibly by
typing into the box.
JList An area where a list of items is displayed from which the
user can make a selection by clicking once on any
element in the list. Double-clicking an element in the list
generates an action event. Multiple elements can be
selected.
JPanel A container in which components can be placed.
So m e ba sic GUI c o m po ne nts.
import java.awt.event.*;
import java.applet.*;
/*
<applet code="FactApplet" width=400 height=300>
</applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends Applet implements KeyListener
{
String msg="";
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyPressed");
}
public void keyReleased(KeyEvent k)
{
showStatus("KeyRealesed");
}
public void keyTyped(KeyEvent k)
{
msg = msg+k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 20, 40);
}
}
2002 Prentice Hall. All rights reserved.
Java Layout Manager 9
The Layout manager is used to layout (or arrange) the GUI java
components inside a Container.
There are many layout managers, but the most frequently used are-
Java BorderLayout
A BorderLayout places components in up to five areas: top, bottom, left,
right, and center.
It is the default layout manager for every java JFrame
Java FlowLayout
FlowLayout is the default layout manager for every JPanel.
It simply lays out components in a single row one after the other.
Java GridBagLayout
It is the more sophisticated of all layouts.
It aligns components by placing them within a grid of cells, allowing
components to span more than one cell.