Professional Documents
Culture Documents
GUI Components:: 2005 Pearson Education, Inc. All Rights Reserved
GUI Components:: 2005 Pearson Education, Inc. All Rights Reserved
11
GUI Components:
Part 1
OBJECTIVES
In this chapter you will learn:
The design principles of graphical user interfaces
(GUIs).
To build GUIs and handle events generated by
user interactions with GUIs.
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
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9
Introduction
Simple GUI-Based Input/Output with JOptionPane
Overview of Swing Components
Displaying Text and Images in a Window
Text Fields and an Introduction to Event Handling with
Nested Classes
Common GUI Event Types and Listener Interfaces
How Event Handling Works
JButton
11.11 JList
11.12 Multiple-Selection Lists
11.13 Mouse Event Handling
11.14 Adapter Classes
11.15
JPanel
FlowLayout
11.17.2
BorderLayout
11.17.3 GridLayout
11.18 Using Panels to Manage More Complex Layouts
11.19
JTextArea
11.20 Wrap-Up
11.1 Introduction
Graphical user interface (GUI)
Presents a user-friendly mechanism for interacting with an
application
Often contains title bar, menu bar containing menus,
buttons and combo boxes
Built from GUI components
button
menus
title bar
menu bar
combo box
scroll
bars
Dialog boxes
Used by applications to interact with the user
Provided by Javas JOptionPane class
Contains input dialogs and message dialogs
Outline
10
Outline
Text field in which
the user types a
value
11
Addition.java
(2 of 2)
title bar
12
13
14
15
16
17
18
19
Lightweight components
Not tied directly to GUI components supported by
underlying platform
Heavyweight components
Tied directly to the local platform
AWT components
Some Swing components
20
21
22
23
24
25
Class JFrame
Most windows are an instance or subclass of this class
Provides title bar
Provides buttons to minimize, maximize and close the
application
26
27
28
Outline
29
LabelFrame.java
(1 of 2)
Outline
30
LabelFrame.java
(2 of 2)
Outline
31
LabelTest.java
32
33
34
35
Interface SwingConstants
Declares a set of common integer constants such as those
used to set the alignment of components
Can be used with methods setHorizontalAlignment
and setVerticalAlignment
36
getHorizontalTextPosition and
setHorizontalTextPosition
For setting and retrieving the horizontal position of the text
displayed in the label
37
38
setSize
Specifies the width and height of the window
setVisible
Determines whether the window is displayed (true) or not
(false)
39
40
Class JTextComponent
Superclass of JTextField
Superclass of JPasswordField
Adds echo character to hide text input in component
Outline
41
TextFieldFrame
.java
(1 of 3)
Outline
42
43
TextFieldFrame
Test if the source of the event
is the
.java
(3 of 3)
Get text from text field
Outline
44
TextFieldTest
.java
(1 of 2)
Outline
45
TextFieldTest
.java
(2 of 2)
46
47
Top-level classes
Not declared within another class
Nested classes
Declared within another class
Non-static nested classes are called inner classes
Frequently used for event handling
48
49
50
51
52
53
Event source
Component from which event originates
Can be determined using method getSource
Text from a JTextField can be acquired using
getActionCommand
Text from a JPasswordField can be acquired using
getPassword
54
Event types
All are subclasses of AWTEvent
Some declared in package java.awt.event
Those specific to Swing components declared in
javax.swing.event
55
56
57
58
59
Registering Events
Every JComponent has instance variable
listenerList
Object of type EventListenerList
Maintains references to all its registered listeners
60
61
Event-Handler Invocation
Events are dispatched to only the event listeners
that match the event type
Events have a unique event ID specifying the event type
62
11.8 JButton
Button
Component user clicks to trigger a specific action
Can be command button, check box, toggle button or radio
button
Button types are subclasses of class AbstractButton
63
64
11.8 JButton
Command button
Generates an ActionEvent when it is clicked
Created with class JButton
Text on the face of the button is called button label
65
66
Outline
67
ButtonFrame.java
(1 of 2)
Outline
68
Outline
69
ButtonTest.java
(1 of 2)
Outline
70
ButtonTest.java
(2 of 2)
71
11.8 JButton
JButtons can have a rollover icon
Appears when mouse is positioned over a button
Added to a JButton with method setRolloverIcon
72
73
74
75
76
11.9.1 JCheckBox
JCheckBox
Contains a check box label that appears to right of check
box by default
Generates an ItemEvent when it is clicked
ItemEvents are handled by an ItemListener
Passed to method itemStateChanged
Outline
77
CheckBoxFrame
.java
(1 of 3)
Declare two JCheckBox instance
variables
Outline
78
itemStateChanged method is
called when a JCheckBox is
clicked
Outline
79
Outline
80
CheckBoxTest
.java
81
11.9.2 JRadioButton
JRadioButton
Has two states selected and unselected
Normally appear in a group in which only one radio button
can be selected at once
Group maintained by a ButtonGroup object
Declares method add to add a JRadioButton to
group
82
Outline
83
RadioButtonFrame.
java
(1 of 3)
Outline
84
RadioButtonFrame
.java
(2 of 3)
Outline
85
(3 of 3)
Outline
86
RadioButtonTest
.java
87
Combo box
Also called a drop-down list
Implemented by class JComboBox
Each item in the list has an index
setMaximumRowCount sets the maximum number of
rows shown at once
JComboBox provides a scrollbar and up and down arrows
to traverse list
88
89
Outline
90
ComboBoxFrame
.java
(1 of 2)
91
Declare method (2 of 2)
itemStateChanged
Test state change of JComboBox
Method getSelectedIndex
locates selected item
Outline
92
ComboBoxTest
.java
scroll arrows
scroll box
93
94
95
11.11 JList
List
Displays a series of items from which the user may select
one or more items
Implemented by class JList
Allows for single-selection lists or multiple-selection lists
A ListSelectionEvent occurs when an item is selected
Handled by a ListSelectionListener and passed to
method valueChanged
Outline
96
ListFrame.java
(1 of 2)
Outline
Create JList
97
ListFrame.java
Set selection mode of JList
(2 of 2)
Outline
98
ListTest.java
99
Outline
100
Multiple
SelectionFrame
.java
(1 of 3)
Outline
101
(2 of 3)
Outline
102
Multiple
Set cell height for presentation
SelectionFrame
.java
(3 of 3)
Set selection model to single
interval selection
Outline
103
Multiple
SelectionTest
.java
104
105
106
107
Outline
108
MouseTracker
Frame.java
(1 of 4)
Outline
Create event handler for mouse
events
109
Outline
110
(3 of 4)
Outline
111
(4 of 4)
Outline
112
MouseTracker
Frame.java
(1 of 2)
Outline
113
MouseTracker
Frame.java
(2 of 2)
114
115
116
Extending MouseAdapter
MouseAdapter
Adapter class for MouseListener and
MouseMotionListener interfaces
Extending class allows you to override only the methods
you wish to use
117
118
package java.awt.event.
Outline
119
MouseDetails
Frame.java
(1 of 2)
Outline
120
MouseDetails
Frame.java
(2 of 2)
Outline
121
MouseDetails
.java
(1 of 2)
Outline
122
MouseDetails
.java
(2 of 2)
123
124
125
Method paintComponent
Method paintComponent
Draws on a Swing component
Overriding method allows you to create custom drawings
Must call superclass method first when overridden
126
127
128
129
Outline
130
PaintPanel.java
(1 of 2)
131
PaintPanel.java
(2 of 2)
132
133
Outline
134
Painter.java
(1 of 2)
Outline
135
Painter.java
(2 of 2)
136
Outline
137
KeyDemoFrame
.java
(1 of 3)
Implement KeyListener
interface
Outline
138
(2 of 3)
Outline
139
KeyDemoFrame
.java
Outline
140
KeyDemo.java
(1 of 2)
Outline
141
KeyDemo.java
(1 of 2)
142
143
144
145
11.17.1 FlowLayout
FlowLayout
Simplest layout manager
Components are placed left to right in order they are
added
Components can be left aligned, centered or right aligned
146
Outline
147
FlowLayoutFrame
.java
(1 of 3)
Create FlowLayout
Outline
148
FlowLayoutFrame
.java
(2 of 3)
Adjust layout
Outline
149
Adjust layout
FlowLayoutFrame
.java
(3 of 3)
Add JButton; FlowLayout
will handle placement
Adjust layout
Outline
150
FlowLayoutDemo
.java
(1 of 2)
Outline
151
FlowLayoutDemo
.java
(2 of 2)
152
11.17.2 BorderLayout
BorderLayout
Arranges components into five regions north, south, east,
west and center
Implements interface LayoutManager2
Provides horizontal gap spacing and vertical gap spacing
153
154
155
Outline
156
BorderLayout
Frame.java
(1 of 2)
Outline
157
BorderLayout
Add buttons to application Frame.java
using
Update layout
Outline
158
BorderLayout
Demo.java
(1 of 2)
horizontal gap
vertical gap
Outline
159
BorderLayout
Demo.java
(2 of 2)
160
11.17.3 GridLayout
GridLayout
Divides container into a grid
Every component has the same width and height
Outline
161
GridLayout
Frame.java
(1 of 2)
Create GridLayout
Set layout
Outline
162
(2 of 2)
Update layout
Outline
163
GridLayoutDemo
.java
164
Outline
165
PanelFrame.java
(1 of 2)
Create JPanel
Set layout
Outline
166
Outline
167
PanelDemo.java
168
11.19 JTextArea
JTextArea
Provides an area for manipulating multiple lines of text
Box container
Subclass of Container
Uses a BoxLayout layout manager
169
Outline
170
TextAreaFrame
.java
(1 of 2)
Declare JTextArea instance
variables
Outline
171
(2 of 2)
Outline
172
TextAreaDemo
.java
(1 of 2)
Outline
173
TextAreaDemo
.java
(2 of 2)
174
Vertical policies
Always (VERTICAL_SCROLLBAR_ALWAYS)
As needed (VERTICAL_SCROLLBAR_AS_NEEDED)
Never (VERTICAL_SCROLLBAR_NEVER)