Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java GUI Programming: Components, Events, Listeners, and Layout Managers, Study notes of Data Structures and Algorithms

An introduction to java gui programming, focusing on the use of components, events, listeners, and layout managers. It covers the basics of creating a java program with a gui, including the necessary packages and the creation of various gui elements such as buttons, text fields, checkboxes, and sliders. The document also discusses the role of layout managers in arranging components within a container.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-wtg-1
koofers-user-wtg-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3/2/09
1
6.1 – GUI Elements
! Command-line applications,
interact with the user through simple prompts and feedback
! … but they lack the rich user experience
! With GUI, the user is not limited to responding to prompts in a
particular order and receiving feedback in one place
! Three kinds of objects are needed to create a GUI in Java
components
events
listeners
6.1 – Components, Events, Listeners
! Component –defines a screen element used to display information
or allow the user to interact with the program
A container is a special type of component that is used to hold and
organize other components
! Event – an object that represents some occurrence in which we may
be interested
Often correspond to user actions (mouse button press, key press)
Most GUI components generate events to indicate a user action related
to that component
Program that is oriented around GUI, responding to user events is called
event-driver
! Listener – an object that “waits” for an event to occur
and responds in way when it does
In designing a GUI-based program
we need to establish the relationships between the listener,
the event it listens for, and
the component that generates the event
6.1 – GUI Elements
! To create a Java program that uses a GUI, we must:
instantiate and set up the necessary components,
implement listener classes that define what happens when
particular events occur, and
establish the relationship between the listeners and the
components that generate the events of interest
! Java components and other GUI-related classes are
defined primarily in two packages
java.awt
javax.swing
6.2 – Components
! A variety of interactive components
Buttons – of course…
text fields –input from keyboard
check boxes – a button that can be
toggled on or off using the mouse
(indicates a boolean value is set or
unset)
radio buttons –to provide
a set of mutually exclusive options
sliders – specify a numeric value
within a bounded range
combo boxes – select one of several
options from a “drop down” menu
pf3

Partial preview of the text

Download Java GUI Programming: Components, Events, Listeners, and Layout Managers and more Study notes Data Structures and Algorithms in PDF only on Docsity!

6.1 – GUI Elements

Command-line applications , interact with the user through simple prompts and feedback … but they lack the rich user experience With GUI, the user is not limited to responding to prompts in a particular order and receiving feedback in one place Three kinds of objects are needed to create a GUI in Java  components  events  listeners

6.1 – Components, Events, Listeners

Component –defines a screen element used to display information or allow the user to interact with the program  A container is a special type of component that is used to hold and organize other components Event – an object that represents some occurrence in which we may be interested  Often correspond to user actions (mouse button press, key press)  Most GUI components generate events to indicate a user action related to that component  Program that is oriented around GUI, responding to user events is called event-driver Listener – an object that “waits” for an event to occur and responds in way when it does  In designing a GUI-based program we need to establish the relationships between the listener , the event it listens for, and the component that generates the event

6.1 – GUI Elements

To create a Java program that uses a GUI, we must:

instantiate and set up the necessary components,  implement listener classes that define what happens when particular events occur, and  establish the relationship between the listeners and the components that generate the events of interest

Java components and other GUI-related classes are

defined primarily in two packages

 java.awt  javax.swing

6.2 – Components

A variety of interactive components  Buttons – of course…text fields –input from keyboard  check boxes – a button that can be toggled on or off using the mouse (indicates a boolean value is set or unset)  radio buttons –to provide a set of mutually exclusive options  sliders – specify a numeric value within a bounded range  combo boxes – select one of several options from a “drop down” menu

6.3 – Layout Managers

Every container is managed by a layout manager object that determines how the components in the container are arranged visually  Every container has a default layout manager, but we can replace it if desired The layout manager is consulted when needed, such as when the container is resized or when a component is added A layout manager determines the size and position of each component Every layout manager has its own rules and properties governing the layout of the components it contains For some layout managers, the order in which you add the components affects their positioning

6.3 – Predefined Layout Managers

Layout Manager Description Border Layout Organizes components into five areas  (North, South, East, West, and Center) Box Layout Organizes components into a single row or column Tabbed Layout Organizes components into one area  such that only one area is visible at a time Flow Layout Organizes components from left to right,  starting new rows as necessary Grid Layout Organizes components into a grid  of rows and columns

6.3 – Flow Layout

The easiest layout managers to use The JPanel class uses flow layout by default Puts as many components as possible on a row, at their preferred size When a component can not fit on a row, it is put on the next row

6.3 – Border Layout

A border layout has five areas to which components can be added: North, South, East, West, and Center The four outer areas are as large as needed in order to accommodate the component they contain If no components are added to a region, the region takes up no room in the overall layout The Center area expands to fill any available space