0% found this document useful (0 votes)
38 views37 pages

Lesson 11 C - Gui Basics With Listeners

This document discusses GUI basics in Java, including design principles, components, and event handling. It covers creating GUIs with Swing components like buttons, labels, lists and text fields. It also describes handling mouse and keyboard events, using layout managers, and creating dialog boxes. Examples are provided to demonstrate displaying text and images in a window, using text fields, and registering event handlers for buttons. The document compares Swing and AWT components and shows the inheritance hierarchy for Swing components.

Uploaded by

Dorothy Jean
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)
38 views37 pages

Lesson 11 C - Gui Basics With Listeners

This document discusses GUI basics in Java, including design principles, components, and event handling. It covers creating GUIs with Swing components like buttons, labels, lists and text fields. It also describes handling mouse and keyboard events, using layout managers, and creating dialog boxes. Examples are provided to demonstrate displaying text and images in a window, using text fields, and registering event handlers for buttons. The document compares Swing and AWT components and shows the inheritance hierarchy for Swing components.

Uploaded by

Dorothy Jean
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/ 37

GUI BASICS

Topics
• Design principles of GUI
• Build GUIs and handle events
• To understand the packages containing GUI
components, event-handling classes and interfaces.
• To create and manipulate buttons, labels, lists, text
fields and panels.
• To handle mouse events and keyboard events.
• To use layout managers to arrange GUI components
GUI design principles
• It has a window
• It has a title bar
• It contains menus(file, edit, etc..)
• It uses buttons
• It uses combo box
• It uses scroll bars
Basic GUI-based input/output
• Using JOptionPane
• Use dialog boxes – used to display important
messages
– JOptionPane.showInputDialog(“ ...”);
– Joptionpane.showMessageDialog(“...”);
Example:
Output
showMessage Dialog Constants
II. Swing Components
• Package javax.swing
• Pure Java
• Part of Java Foundation Classes
Swing vs AWT
• Swing - are lightweight component, not tied
with the platform on which application
executes
• AWT(Abstract Windowing Tool) – heavyweight
component, tied with local platform’s
windowing system
Inheritance Hierarchy of Swing Components

• JComponent is a subclass of a container –


different swing objects
Example:
Displaying text
and images in
a window
Imports from GUIFrame class
Instance variables
constructor
Adding label2 & label 3
GUITest Class
Output
Use of Text fields and Event Handling
• GUIs are event-driven
• Event – when the user interacts with GUI
– Clicking a mouse, typing, select an item from the
menu, closing a window or moving the mouse
• Event handlers – the code that performs a task
in response to an event
imports
JTextField declaration
constructor
Cont...
Cont... Register an event handler before the
end of the constructor
Class TextFieldHandler
TextFieldHandler cont..
Steps to set up event handling
1. Create the class that represent the handler
2. Implement an appropriate interface – event-
listener interface
3. Indicate that an object of the class from Steps
1 and 2 should be notified when the event
occurs. This is known as registering the event
handler
Nested classes
• A class declaration inside a class. Non static classes
• Need an object of the top level class that contains in an
inner class
• Used by event handlers
• GUI components can generate a variety of events, and
each event is represented by a class and can be
processed only by its appropriate handler
• Example: actionEvent is generated
• Can be processed by an object that implements the
interface of actionListener
To handle the events
• Innerclass implements the interface
actionListener and declare one method
actionPerformed()
Output:
Some event classes
Some Event
Listeners
The JButton
Button
• A component that user clicks to trigger a
specific action
• Types of button
– Command
– Checkboxes
– Toggle
– radio
Swing Button Hierarchy
Command button
• Generates an action event
• Use the class JButton
• Text on the face of a JButton is a button Label

You might also like