JAVA PROGRAMMING
WITH GUI PART I
Object Oriented Programming
What is GUI
2
Stands for Graphical User Interface
Program interface that takes advantage
of the computers graphics capabilities
to make the program easier to use
Designed in 1970 by Xerox Corporations
Palo Alto Research Center
JAVA PROGRAMMING WITH GUI PART I
What is GUI
3
Basic Components:
Pointer
Pointing device
Icons
Desktop
Windows
Menu
JAVA PROGRAMMING WITH GUI PART I
Basic GUI Objects
4
In Java, GUI-based programs are
implemented by using the classes from
the standard
javax.swing (Swing classes) and
java.awt (AWT classes) packages.
All GUI classes are based on the
fundamental classes in the Application
Windowing Toolkit or AWT.
JAVA PROGRAMMING WITH GUI PART I
Basic GUI Objects
5
A GUI program is composed of three
types of software:
Graphical
components that make up the
Graphical User Interface
Listener methods that receive the events and
respond to them
Application methods that perform the desired
operations on the information
JAVA PROGRAMMING WITH GUI PART I
Abstract Window Toolkit
6
Javas platform-independent windowing,
graphics, and user-interface widget
toolkit
Part of the Java Foundation Classes (JFC)
the standard API (Application Program
Interface) for providing graphical user
interfaces for Java programs
Contains the fundamental classes used
for constructing GUIs
JAVA PROGRAMMING WITH GUI PART I
Swing
7
Graphical user interface toolkit in Java, which is
one part of the Java Foundation Classes (JFC)
Includes graphical user interface (GUI) widgets
such as text boxes, buttons, split-panes, and
tables runs the same on all platforms
Supports pluggable look and feel not by using
the native platform's facilities, but by roughly
emulating them
Classes are often described as lightweight because
they do not require allocation of native resources
in the operating systems window toolkit
JAVA PROGRAMMING WITH GUI PART I
Swing Components
8
UI elements such as dialog boxes and buttons; you
can usually recognize their names because they
begin with J
Each Swing component is a descendant of a
JComponent, which in turn inherits from the
java.awt.Container class
You insert the import statement
import javax.swing.*;
at the beginning of your Java program files so
you can take advantage of the Swing UI
components and their methods
JAVA PROGRAMMING WITH GUI PART I
Using the JFrame Class
9
contains the most basic functionalities
that support features found in any frame
window, such as minimizing the window,
moving the window, and resizing the
window
To design such a frame window, we
define a subclass of the JFrame class and
add methods and data members that
implement the needed functionalities.
JAVA PROGRAMMING WITH GUI PART I
Using the JFrame Class
10
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
JAVA PROGRAMMING WITH GUI PART I
Using the JFrame Class
11
isResizable()
setVisible(boolean)
indicate whether the JFrame is resizable Sets a JFrame
to be visible void
using the boolean argument true and invisible using
the boolean argument false
void setBounds(int, int, int, int)
Overrides the default behavior of the JFrame to be
positioned in the upper-left corner of the computer
screens desktop. The first two arguments are the
horizontal and vertical positions of the JFrames upperleft corner on the desktop. The final two arguments set
the width and height.
JAVA PROGRAMMING WITH GUI PART I
Using the JFrame Class
12
When a JFrame serves as a Swing applications main user
interface, you usually want the program to exit when the
user clicks Close. To change this behavior, you can call a
JFramessetDefaultCloseOperation() method and
use one of the following four values as an argument:
JFrame.EXIT_ON_CLOSE() - exits the program when the JFrame is
closed.
WindowConstants.DISPOSE_ON_CLOSE() - closes the frame,
disposes of the JFrame object, and keeps running the application.
WindowConstants.DO_NOTHING_ON_CLOSE() - keeps the JFrame
and continues running. In other words, it functionally disables the
Close button
WindowConstants.HIDE_ON_CLOSE () - closes the JFrame and
continues running; this is the default operation that you frequently
want to override.
JAVA PROGRAMMING WITH GUI PART I
Using the JPanel Class
13
The simplest Swing container plain, borderless
surface that can hold lightweight UI components,
such as JButton, JCheckBoxes, or even other
JPanels
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
JAVA PROGRAMMING WITH GUI PART I
Using the JPanel Class
14
To add a component to a JPanel, the
containers add() method is called, using
the component as the argument.
In using a JPanel to hold components, the
setContentPane() method is also
used to allow the JPanel to be set as a
content pane so it can hold components.
JAVA PROGRAMMING WITH GUI PART I