java chapter 15
TRANSCRIPT
-
7/27/2019 Java chapter 15
1/231
(C) 2010 Pearson Education, Inc. All rights reserved.
Java How to Program, 8/e
-
7/27/2019 Java chapter 15
2/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
3/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
4/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
5/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A graphical user interface (GI)presents a user!
friendl" #echanis# for interacting $ith an application.
Pronounced %G&&!ee'
Gives an application a distinctive %loo' and %feel.'
Consistent, intuitive user!interface co#ponents give users a
sense of fa#iliarit"
earn ne$ applications #ore *uicl" and use the# #ore
productivel".
-
7/27/2019 Java chapter 15
6/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
7/231(C) 2010 Pearson Education, Inc. !! rig"ts re
+uilt fro# GI co#ponents.
o#eti#es called controlsor $idgets-short for $indo$
gadgets.
ser interacts via the #ouse, the e"oard or another
for# of input, such as voice recognition.
I/Es
Provide GI design tools to specif" a co#ponents eact sie
and location in a visual #anner " using the #ouse.
Generates the GI code for "ou.
Greatl" si#plifies creating GIs, ut each I/E has different
capailities and generates different code.
-
7/27/2019 Java chapter 15
8/231(C) 2010 Pearson Education, Inc. !! rig"ts re
Ea#ple of a GI3 $inget4 application (5ig. 17.1)
http://download.java.net/javadesktop/swingset3/Swin
title arat top contains the $indo$s title.
#enu arcontains #enus(Fileand View). In the top!right region of the $indo$ is a set ofuttons
8"picall", users press uttons to perfor# tass.
In the GUI Componentsarea of the $indo$ is a co#o
o9
ser can clic the do$n arro$ at the right side of the o to select
fro# a list of ite#s.
http://download.java.net/javadesktop/swingset3/SwingSet3.jnlphttp://download.java.net/javadesktop/swingset3/SwingSet3.jnlp -
7/27/2019 Java chapter 15
9/231(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
10/231(C) 2010 Pearson Education, Inc. !! rig"ts re
:ava E ; update 10
-
7/27/2019 Java chapter 15
11/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8hree $a"s to use
-
7/27/2019 Java chapter 15
12/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8o set sinstallation folder. If "ou are using an I/E that depends on the :/> (e.g.,
-
7/27/2019 Java chapter 15
13/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8o select
-
7/27/2019 Java chapter 15
14/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Bost applications use $indo$s or dialog oes(also called
dialogs) to interact $ith the user.
JOptionPane(pacage java".swing) providespreuilt dialog oes for input and output
/ispla"ed via static%&ption'ane#ethods.
5igure 17.2 uses t$o input dialogsto otain integers fro#
the user and a #essage dialogto displa" the su# of the
integers the user enters.
-
7/27/2019 Java chapter 15
15/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
16/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
17/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
18/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%&ption'anestatic#ethod showInputDialogdispla"s an input dialog, using the #ethods Stringargu#entas a pro#pt. 8he user t"pes characters in the tet field, then clics OKor presses the
Entere" to su#it the Stringto the progra#. Clicing OKdis#isses (hides)the dialog. Can input onl" Strings. 8"pical of #ost GI co#ponents. If the user clics Cancel, returns null.%&ption'anedialog are dialog-the user cannot interact $ith the rest
of the application $hile dialog is displa"ed.
-
7/27/2019 Java chapter 15
19/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
20/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
21/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Converting Strings to intalues (ntegerclasss static#ethod parse(ntconverts its Stringargu#ent
to an intvalue. Bessage /ialogs
%&ption'anestatic#ethod showMessageDialogdispla"s a #essagedialog.
8he first argu#ent helps deter#ine $here to position the dialog. If null, the dialog o is displa"ed at the center of "our screen. 8he second argu#ent is the #essage to displa". 8he third argu#ent is the Stringthat should appear in the title ar at the top
of the dialog. 8he fourth argu#ent is the t"pe of #essage dialog to displa".
-
7/27/2019 Java chapter 15
22/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Bessage /ialogs A JOption-Pane.PLAIN_MESSAGE dialog does not
displa" an icon to the left of the #essage.
%&ption'aneonline docu#entation3http://java.sun.com/javase/!/docs/api/java"/s
http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html -
7/27/2019 Java chapter 15
23/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
24/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
25/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
$ing GI co#ponentslocated in pacagejavax.swing.
Bost are pure :ava co#ponents =ritten, #anipulated and displa"ed co#pletel" in :ava.
Part of the :ava 5oundation Classes (:5C)for cross!platfor# GIdevelop#ent.
:5C and :ava destop technologies3http://java.sun.com/javase/technologies/desktop/
http://java.sun.com/javase/technologies/desktop/http://java.sun.com/javase/technologies/desktop/ -
7/27/2019 Java chapter 15
26/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
27/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Astract =indo$ 8oolit (A=8)in pacage java.awtisanother set of GI co#ponents in :ava. =hen a :ava application $ith an A=8 GI eecutes on different :ava
platfor#s, the applications GI co#ponents displa" differentl" on eachplatfor#.
8ogether, the appearance and the $a" in $hich the user interacts$ith the application are no$n as that applications loo!and!feel.
$ing GI co#ponents allo$ "ou to specif" a unifor# loo!and!feel for "our application across all platfor#s or to use each
platfor#s custo# loo!and!feel.
-
7/27/2019 Java chapter 15
28/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
29/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Bost $ing co#ponents are not tied to actual GIco#ponents of the underl"ing platfor#. >no$n as light$eight co#ponents.
A=8 co#ponents are tied to the local platfor# and are
called heav"$eight co#ponents, ecause the" rel" on thelocal platfor#s $indo$ing s"ste#to deter#ine theirfunctionalit" and their loo!and!feel.
everal $ing co#ponents are heav"$eight co#ponents.
-
7/27/2019 Java chapter 15
30/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
31/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
32/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Class o!ponent(pacage java.awt) declares#an" of the attriutes and ehaviors co##on to theGI co#ponents in pacages java.awtandjava".swing.
Bost GI co#ponents etend class )omponentdirectl" or indirectl".
&nline docu#entation3http://java.sun.com/javase/!/docs/api/java/aw
http://java.sun.com/javase/6/docs/api/java/awt/Component.htmlhttp://java.sun.com/javase/6/docs/api/java/awt/Component.html -
7/27/2019 Java chapter 15
33/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
34/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Class ontaine"(pacage java.awt) is a suclass of)omponent. )omponents are attached to )ontainers so that the" can e
organied and displa"ed on the screen. An" oDect that is a )ontainercan e used to organie other
)omponents in a GI. +ecause a )ontaineris a )omponent, "ou can place)ontainers in other )ontainers to help organie a GI.
&nline docu#entation3http://java.sun.com/javase/!/docs/api/java/awt/)onta
http://java.sun.com/javase/6/docs/api/java/awt/Container.htmlhttp://java.sun.com/javase/6/docs/api/java/awt/Container.html -
7/27/2019 Java chapter 15
35/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Class Jo!ponent(pacage java".swing) is asuclass of )ontainer.
%)omponentis the superclass of all light$eight$ing co#ponents, all of $hich are also
)ontainers.
-
7/27/2019 Java chapter 15
36/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
o#e co##on light$eight co#ponent features supported" %)omponentinclude3pluggable look- and- feelShortcut keys (called mnemonics!"ommon event- handling capabilities for components that initiate the
same actions in an application.
tool tipsSupport for accessibilitySupport for user- interface locali#ation
&nline docu#entation3http://java.sun.com/javase/!/docs/api/java"#/s
wing/%)omponent.html
-
7/27/2019 Java chapter 15
37/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Bost $indo$s that can contain $ing GI co#ponents areinstances of class %rameor a suclass of %rame.
%rameis an indirect suclass of class java.awt.+indow Provides the asic attriutes and ehaviors of a $indo$
a title ar at the top
uttons to #ini#ie, #ai#ie and close the $indo$ Bost of our ea#ples $ill consist of t$o classes
a suclass of %ramethat de#onstrates ne$ GI concepts an application class in $hich maincreates and displa"s the
applications pri#ar" $indo$.
-
7/27/2019 Java chapter 15
38/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
39/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
40/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
41/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
42/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
In a large GI /ifficult to identif" the purpose of ever" co#ponent.
Provide tet stating each co#ponents purpose.
uch tet is no$n as a laeland is created $ith class
JLa#el-a suclass of %)omponent. /ispla"s read!onl" tet, an i#age, or oth tet and an i#age.
-
7/27/2019 Java chapter 15
43/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%rames constructor uses its Stringargu#ent as thetet in the $indo$s title ar.
Bust attach each GI co#ponent to a container, such as a%rame.
ou t"picall" #ust decide $here to position each GIco#ponent. >no$n as specif"ing the la"out of the GI co#ponents.
:ava provides several la"out #anagersthat can help "ou positionco#ponents.
-
7/27/2019 Java chapter 15
44/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Ban" I/Es provide GI design tools in $hich "ou canspecif" the eact sie and location of a co#ponent
I/E generates the GI code for "ou Greatl" si#plifies GI creation 8o ensure that this oos ea#ples can e used $ith an"
I/E, $e did not use an I/E to create the GI code =e use :avas la"out #anagers in our GI ea#ples
-
7/27/2019 Java chapter 15
45/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
$lowLa%out GI co#ponents are placed on a container fro# left to right in the order in
$hich the progra# attaches the# to the container. =hen there is no #ore roo# to fit co#ponents left to right, co#ponents
continue to displa" left to right on the net line. If the container is resied, a lowLa,outreflo$s the co#ponents to
acco##odate the ne$ $idth of the container, possil" $ith fe$er or #ore ro$s
of GI co#ponents. Bethod setLa%outis inherited fro# class )ontainer.
argu#ent #ust e an oDect of a class that i#ple#ents the La,out-anagerinterface (e.g., lowLa,out).
-
7/27/2019 Java chapter 15
46/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
47/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
48/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%Labelconstructor can receive a Stringspecif"ing thelaels tet.
Bethod set&ool&ip&ext(inherited " %Labelfro#%)omponent) specifies the tool tip that is displa"ed $henthe user positions the #ouse cursor over a %)omponent(such as a %Label).
ou attach a co#ponent to a container using the a''#ethod, $hich is inherited indirectl" fro# class)ontainer.
-
7/27/2019 Java chapter 15
49/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Icons enhance the loo!and!feel of an application and are alsoco##onl" used to indicate functionalit".
An icon is nor#all" specified $ith an I(onargu#ent to aconstructor or to the co#ponents setI(on#ethod.
An (conis an oDect of an" class that i#ple#ents interface
(con(pacage java".swing). I!ageI(on(pacage java".swing) supports several i#age
for#ats, including Graphics Interchange 5or#at (GI5), Portale
-
7/27/2019 Java chapter 15
50/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
get)lass.get0esource1bug2.png1 Invoes #ethod getlass(inherited indirectl" fro# class &bject)
to retrieve a reference to the )lassoDect that represents theLabelrameclass declaration.
-
7/27/2019 Java chapter 15
51/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A %Labelcan displa" an (con. %Labelconstructor can receive tet and an (con.
8he last constructor argu#ent indicates the Dustification of the laelscontents.
Interface Swingonstants(pacage java".swing) declares a setof co##on integer constants (such as Swing)onstants.L4) that
are used $ith #an" $ing co#ponents. +" default, the tet appears to the right of the i#age $hen a lael
contains oth tet and an i#age. 8he horiontal and vertical align#ents of a %Labelcan e set $ith
#ethods set*o"i+ontalAlign!entandset,e"ti(alAlign!ent, respectivel".
-
7/27/2019 Java chapter 15
52/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
53/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Class %Labelprovides #ethods to change a laels appearanceafter it has een instantiated. Bethod set&extsets the tet displa"ed on the lael. Bethod get&extretrievesF the current tet displa"ed on a lael. Bethod setI(onspecifies the (conto displa" on a lael.
Bethod getI(onretrievesF the current (condispla"ed on alael. Bethods set*o"i+ontal&extPositionandset,e"ti(al&extPositionspecif" the tet position in thelael.
-
7/27/2019 Java chapter 15
54/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
+" default, closing a $indo$ si#pl" hides the $indo$. Calling #ethod setDeaultloseOpe"ation(inherited
fro# class %rame) $ith the argu#entJ$"a!e.EI&_ON_LOSEindicates that the progra# shouldter#inate $hen the $indo$ is closed " the user.
Bethod setSi+especifies the $idth and height of the $indo$in piels.
Bethod set,isi#le$ith the argu#ent truedispla"s the$indo$ on the screen.
-
7/27/2019 Java chapter 15
55/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
GIs are event driven. =hen the user interacts $ith a GI co#ponent, the
interaction-no$n as an event-drives the progra# to
perfor# a tas.
8he code that perfor#s a tas in response to an event is
called an event handler, and the overall process of
responding to events is no$n as event handling.
-
7/27/2019 Java chapter 15
56/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
J&ext$iel'sand JPasswo"'$iel's(pacagejava".swing).
%4e"tieldetendsclass J&exto!ponent(pacagejava".swing.te"t), $hich provides #an" features co##onto $ings tet!ased co#ponents.
Class %'asswordieldetends %4e"tieldand adds#ethods that are specific to processing pass$ords.
%'asswordieldsho$s that characters are eing t"ped as theuser enters the#, ut hides the actual characters $ith an echocharacter.
-
7/27/2019 Java chapter 15
57/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
58/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
59/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
60/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
61/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
62/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
63/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
64/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
=hen the user t"pes data into a %4e"tieldor a%'asswordield, then pressesEnter, an eventoccurs.
ou can t"pe onl" in the tet field that is %in focus.'
A co#ponent receives the focus $hen the user clics
the co#ponent.
-
7/27/2019 Java chapter 15
65/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
+efore an application can respond to an event for aparticular GI co#ponent, "ou #ust perfor# several
coding steps3"reate a class that represents the event handler.
$mplement an appropriate interface% known as an event-listener interface% in the class from St ep 1.
$ndicate that an ob&ect of the class from Steps ' and 2 shouldbe notified when the event occurs. his is known as registeringthe event handler.
-
7/27/2019 Java chapter 15
66/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
All the classes discussed so far $ere so!called top!levelclasses-that is, the" $ere not declared inside another
class.
:ava allo$s "ou to declare classes inside other classes
-these are called nested classes. Can e staticor non!static.
-
7/27/2019 Java chapter 15
67/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
68/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
+efore an oDect of an inner class can e created, there #ust firste an oDect of the top!level class that contains the inner class.
8his is re*uired ecause an inner!class oDect i#plicitl" has areference to an oDect of its top!level class.
8here is also a special relationship et$een these oDects-the
inner!class oDect is allo$ed to directl" access all the varialesand #ethods of the outer class.
A nested class that is staticdoes not re*uire an oDect of itstop!level class and does not i#plicitl" have a reference to anoDect of the top!level class.
-
7/27/2019 Java chapter 15
69/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Inner classes can e declared public, protectedor private.
ince event handlers tend to e specific to the
application in $hich the" are defined, the" are often
i#ple#ented as privateinner classes.
-
7/27/2019 Java chapter 15
70/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
GI co#ponents can generate #an" events in responseto user interactions.
Each event is represented " a class and can e
processed onl" " the appropriate t"pe of event
handler.
-
7/27/2019 Java chapter 15
71/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
=hen the user pressesEnter in a %4e"tieldor%'asswordield, an A(tionEvent(pacagejava.awt.event) occurs.
Processed " an oDect that i#ple#ents the interfaceA(tionListene"(pacage java.awt.event).
8o handle Actionvents, a class #ust i#ple#entinterface ActionListenerand declare #ethodaction'erformed. 8his #ethod specifies the tass to perfor# $hen an Actionvent
occurs.
-
7/27/2019 Java chapter 15
72/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
73/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
74/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Bust register an oDect as the event handler for eachtet field.
a''A(tionListene"registers an
ActionListeneroDect to handle
Actionvents. After an event handler is registered the oDect listens
for events.
-
7/27/2019 Java chapter 15
75/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8he GI co#ponent $ith $hich the user interacts is theevent source. Actionvent#ethod getSource(inherited fro# classvent&bject) returns a reference to the event source.
Actionvent#ethod getA(tiono!!an'otains
the tet the user t"ped in the tet field that generated theevent. %'asswordield#ethod getPasswo"'returns the
pass$ords characters as an arra" of t"pe char.
-
7/27/2019 Java chapter 15
76/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
77/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
5igure 17.11 illustrates a hierarch" containing #an"event classes fro# the pacage java.awt.event.
sed $ith oth A=8 and $ing co#ponents.
Additional event t"pes that are specific to $ing GI
co#ponents are declared in pacage
javax.swing.event.
-
7/27/2019 Java chapter 15
78/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
79/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
/elegation event #odel-an events processing is delegated toan oDect (the event listener) in the application. 5or each event!oDect t"pe, there is t"picall" a corresponding
event!listener interface. Ban" event!listener t"pes are co##on to oth $ing and A=8
co#ponents. uch t"pes are declared in pacage java.awt.event, and so#e of
the# are sho$n in 5ig. 17.12. Additional event!listener t"pes that are specific to $ing
co#ponents are declared in pacage java".swing.event.
-
7/27/2019 Java chapter 15
80/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Each event!listener interface specifies one or #oreevent!handling #ethods that #ust e declared in the
class that i#ple#ents the interface.
=hen an event occurs, the GI co#ponent $ith $hich
the user interacted notifies its registered listeners "calling each listeners appropriate event!handling
#ethod.
-
7/27/2019 Java chapter 15
81/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
o$ the event!handling #echanis# $ors3 Ever" %)omponenthas a variale listenerListthat
refers to an EventListene"List(pacagejava".swing.event).
Baintains references to registered listeners in the
listenerList. =hen a listener is registered, a ne$ entr" is placed in the
co#ponents listenerList. Ever" entr" also includes the listeners t"pe.
-
7/27/2019 Java chapter 15
82/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
83/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
o$ does the GI co#ponent no$ to callaction'erformedrather than another #ethodH Ever" GI co#ponent supports several event t"pes, including #ouse
events, e" eventsand others.
=hen an event occurs, the event is dispatchedonl" to the event
listeners of the appropriate t"pe. /ispatching is si#pl" the process " $hich the GI co#ponent calls
an event!handling #ethod on each of its listeners that are registeredfor the event t"pe that occurred.
-
7/27/2019 Java chapter 15
84/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Each event t"pe has one or #ore corresponding event!listenerinterfaces.Actionvents are handled " ActionListeners MouseEventsare handled " MouseListene"sandMouseMotionListene"s
/e%Eventsare handled " /e%Listene"s
=hen an event occurs, the GI co#ponent receives (fro# the:B) a uni*ue event I/specif"ing the event t"pe. 8he co#ponent uses the event I/ to decide the listener t"pe to $hich the
event should e dispatched and to decide $hich #ethod to call on eachlistener oDect.
-
7/27/2019 Java chapter 15
85/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
5or an Actionvent, the event is dispatched to ever"registered ActionListeners action'erformed#ethod.
5or a -ouse#vent, the event is dispatched to ever"
registered -ouseListeneror-ouse-otionListener, depending on the #ouse eventthat occurs.
8he -ousevents event I/ deter#ines $hich of the several#ouse event!handling #ethods are called.
-
7/27/2019 Java chapter 15
86/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Auttonis a co#ponent the user clics to trigger a specificaction. everal t"pes of uttons
co##and uttons checoes
toggle uttons radio uttons +utton t"pes are suclasses of A#st"a(t0utton
(pacage java".swing), $hich declares the co##onfeatures of $ing uttons.
-
7/27/2019 Java chapter 15
87/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
88/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
89/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A co##and utton generates an Actionvent$henthe user clics it.
Co##and uttons are created $ith class J0utton.
8he tet on the face of a %5uttonis called auttonlael.
-
7/27/2019 Java chapter 15
90/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
91/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
92/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
93/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
94/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
95/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A %5uttoncan displa" an (con. A %5uttoncan also have a rolloverI(on displa"ed $hen the user positions the #ouse over the %5utton. 8he icon on the %5uttonchanges as the #ouse #oves in and out
of the %5uttons area on the screen. Abstract5utton#ethod
set)ollove"I(on
specifies the i#age displa"ed on the %5utton$hen theuser positions the #ouse over it.
-
7/27/2019 Java chapter 15
96/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
97/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
98/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%5uttons, lie %4e"tields, generateActionvents that can e processed " an"ActionListeneroDect.
-
7/27/2019 Java chapter 15
99/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
100/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8hree t"pes of state uttons-J&oggle0utton,Jhe(10oxand J)a'io0utton-that have onoffor truefalse values.
Classes %)heck5o"and %0adio5uttonare
suclasses of %4oggle5utton. %0adio5uttons are grouped together and are#utuall" eclusive-onl" one in the group can eselected at an" ti#e
-
7/27/2019 Java chapter 15
101/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%4e"tield#ethod set$ont(inherited " %4e"tieldindirectl" fro#class )omponent) sets the font of the %4e"tieldto a ne$ $ont(pacage java.awt).
Stringpassed to the %)heck5o"constructor is the checo laelthatappears to the right of the %)heck5o"" default.
=hen the user clics a %)heck5o", an Ite!Eventoccurs. andled " an Ite!Listene"oDect, $hich #ust i#ple#ent #ethod
ite!Statehange'. An (temListeneris registered $ith #ethod a''Ite!Listene". %)heck5o"#ethod isSele(te'returns trueif a %)heck5o"is
selected.
-
7/27/2019 Java chapter 15
102/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
103/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
104/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
105/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
106/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
107/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
?adio uttons(declared $ith class %0adio5utton) are si#ilarto checoes in that the" have t$o states-selected and notselected (also called deselected).
?adio uttons nor#all" appear as a groupin $hich onl" oneutton can e selected at a ti#e.
electing a different radio utton forces all others to e
deselected. sed to represent #utuall" eclusiveoptions. 8he logical relationship et$een radio uttons is #aintained " a0uttonG"oupoDect (pacage java".swing), $hichorganies a group of uttons and is not itself displa"ed in a user
interface.
-
7/27/2019 Java chapter 15
108/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
109/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
110/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
111/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
112/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
113/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
114/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
115/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
5utton6roup#ethod a''associates a%0adio5utton$ith the group.
If #ore than one selected %0adio5uttonoDect isadded to the group, the selected one that $as added
first $ill e selected $hen the GI is displa"ed. %0adio5uttons, lie %)heck5o"es, generate(temvents $hen the" are cliced.
-
7/27/2019 Java chapter 15
116/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A co#o o(or drop!do$n list) enales the user toselect one ite# fro# a list.
Co#o oes are i#ple#ented $ith class
Jo!#o0ox, $hich etends class %)omponent. %)ombo5o"es generate (temvents.
-
7/27/2019 Java chapter 15
117/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
118/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
119/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
120/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
121/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
122/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8he first ite# added to a %)ombo5o"appears as the currentl"selected ite# $hen the %)ombo5o"is displa"ed.
&ther ite#s are selected " clicing the %)ombo5o", thenselecting an ite# fro# the list that appears.
%)ombo5o"#ethod setMaxi!u!)owountsets the
#ai#u# nu#er of ele#ents that are displa"ed $hen the userclics the %)ombo5o". If there are additional ite#s, the %)ombo5o"provides a
scrollarthat allo$s the user to scroll through all the ele#ents inthe list.
-
7/27/2019 Java chapter 15
123/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
124/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
An anon"#ous inner classis an inner class that is declared$ithout a na#e and t"picall" appears inside a #ethoddeclaration.
As $ith other inner classes, an anon"#ous inner class canaccess its top!level classs #e#ers.
An anon"#ous inner class has li#ited access to the localvariales of the #ethod in $hich its declared. ince an anon"#ous inner class has no na#e, one oDect of
the anon"#ous inner class #ust e created at the point$here the class is declared.
-
7/27/2019 Java chapter 15
125/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
126/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%)ombo5o"#ethod getSele(te'In'exreturnsthe inde of the selected ite#. 5or each ite# selected fro# a %)ombo5o", another
ite# is first deselected-so t$o (temvents occur$hen an ite# is selected.
(temvent#ethod getStatehangereturns thet"pe of state change. (temvent.SL)4$indicates that an ite# $as selected.
-
7/27/2019 Java chapter 15
127/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
128/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A list displa"s a series of ite#s fro# $hich the user#a" select one or #ore ite#s. ists are created $ith cla%List, $hich directl"
etends class %)omponent.ss
upports single!selection lists(onl" one ite# to eselected at a ti#e) and #ultiple!selection lists(an"nu#er of ite#s to e selected).
%Lists generate ListSele(tionEvents insingle!selection lists.
-
7/27/2019 Java chapter 15
129/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
130/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
131/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
132/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
133/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
set,isi#le)owountspecifies the nu#er of ite#s visilein the list.
setSele(tionMo'especifies the lists selection #ode. Class ListSele(tionMo'el(of pacage java".swing)
declares selection!#ode constants SINGLE_SELE&ION(onl" one ite# to e selected at a ti#e)
SINGLE_IN&E),AL_SELE&ION(allo$s selection of severalcontiguous ite#s)
M2L&IPLE_IN&E),AL_SELE&ION(does not restrict the ite#s thatcan e selected).
-
7/27/2019 Java chapter 15
134/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
nlie a %)ombo5o", a %Listdoes notprovide ascrollar if there are #ore ite#s in the list than thenu#er of visile ro$s. A JS("ollPaneoDect is used to provide the scrolling
capailit".
a''ListSele(tionListene"registers aListSele(tionListene"(pacagejava".swing.event) as the listener for a%Listsselection events.
-
7/27/2019 Java chapter 15
135/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Each %rameactuall" consists of three la"ers-the acground, the content
pane and the glass pane. 8he content pane appears in front of the acground and is $here the GI
co#ponents in the %rameare displa"ed. 8he glass pane is displa"s tool tips and other ite#s that should appear in front
of the GI co#ponents on the screen. 8he content pane co#pletel" hides the acground of the %rame.
8o change the acground color ehind the GI co#ponents, "ou #ust changethe content panes acground color.
Bethod get)ontent'anereturns a reference to the %rames content pane(an oDect of class )ontainer).
List#ethod getSele(te'In'exreturns the selected ite#s inde.
-
7/27/2019 Java chapter 15
136/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A #ultiple!selection listenales the user to select #an" ite#s fro# a
%List. A S(N6L7(N408AL7SL)4(&Nlist allo$s selecting a
contiguous range of ite#s. 8o do so, clic the first ite#, then press and hold the Shift e" $hile clicing the
last ite# in the range. A -9L4('L7(N408AL7SL)4(&Nlist (the default) allo$s
continuous range selection as descried for aS(N6L7(N408AL7SL)4(&Nlist and allo$s #iscellaneousite#s to e selected " pressing and holding the Ctrl e" $hile clicingeach ite# to select. 8o deselect an ite#, press and hold the Ctrl e" $hile clicing the ite# a second
ti#e.
-
7/27/2019 Java chapter 15
137/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
138/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
139/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
140/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
141/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
If a %Listdoes not contain ite#s it $ill not dipla" in a
lowLa,out. use %List#ethods set$ixe'ell3i'thand set$ixe'ell*eightto
set the ite# $idth and height 8here are no events to indicate that a user has #ade #ultiple selections
in a #ultiple!selection list. An event generated " another GI co#ponent (no$n as an eternal event)
specifies $hen the #ultiple selections in a %Listshould e processed. Bethod setListDatasets the ite#s displa"ed in a %List. -ethod getSele(te',aluesreturns an arra" of &bjects
representing the selected ite#s in a %List.
-
7/27/2019 Java chapter 15
142/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
MouseListene"and MouseMotionListene"event!listener interfaces for handling #ouse events. An" GI co#ponent
Pacage java".swing.eventcontains interfaceMouseInputListene", $hich etends interfaces-ouseListenerand -ouse-otionListenerto create asingle interface containing all the #ethods.
-ouseListenerand -ouse-otionListener#ethodsare called $hen the #ouse interacts $ith a )omponentifappropriate event!listener oDects are registered for that)omponent
.
-
7/27/2019 Java chapter 15
143/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
144/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
145/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Each #ouse event!handling #ethod receives a MouseEvent
oDect that contains infor#ation aout the #ouse event thatoccurred, including thex- and"!coordinatesof the location$here the event occurred.
Coordinates are #easured fro# the upper!left corner of the GIco#ponent on $hich the event occurred.
8he !coordinates start at 0 and increase fro# left to right. 8he y!coordinates start at 0 and increase fro# top to otto#. 8he #ethods and constants of class InputEvent(-ouse#vents superclass) enale "ou to deter#ine $hich #ouseutton the user cliced.
-
7/27/2019 Java chapter 15
146/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
147/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Interface Mouse3heelListene"enalesapplications to respond to the rotation of a #ouse$heel.
Bethod !ouse3heelMove'receives aMouse3heelEventas its argu#ent.
Class -ouse+heelvent(a suclass of -ouse#vent) contains #ethods that enale the event handlerto otain infor#ation aout the a#ount of $heelrotation.
-
7/27/2019 Java chapter 15
148/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
149/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
150/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
151/231
(C) 2010 Pearson Education Inc !! rig"ts re
-
7/27/2019 Java chapter 15
152/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
153/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
154/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
0o"'e"La%outarranges co#ponents into five regions3NO)&*, SO2&*, EAS&, 3ES&and EN&E).
5orderLa,outsies the co#ponent in the )N40to use allavailale space that is not occupied
Bethods a''MouseListene"and
a''MouseMotionListene"register -ouseListenersand -ouse-otionListeners, respectivel". -ousevent#ethods getand get4return thex-andy!
coordinates of the #ouse at the ti#e the event occurred.
-
7/27/2019 Java chapter 15
155/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Ban" event!listener interfaces contain #ultiple #ethods.
An adapter classi#ple#ents an interface and provides a
default i#ple#entation ($ith an e#pt" #ethod od") of
each #ethod in the interface.
ou etend an adapter class to inherit the defaulti#ple#entation of ever" #ethod and override onl" the
#ethod(s) "ou need for event handling.
-
7/27/2019 Java chapter 15
156/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
157/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
158/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
159/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
160/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
161/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
162/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
163/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A #ouse can have one, t$o or three uttons. Class -ouseventinherits several #ethods fro#(nputventthat can distinguish a#ong #ouse
uttons or #i#ic a #ultiutton #ouse $ith a co#ined
e"stroe and #ouse!utton clic. :ava assu#es that ever" #ouse contains a left #ouse
utton.
-
7/27/2019 Java chapter 15
164/231
-
7/27/2019 Java chapter 15
165/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
166/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8he nu#er of consecutive #ouse clics is returned "-ousevent#ethod getli(1ount. Bethods isMetaDownand isAltDowndeter#ine
$hich #ouse utton the user cliced.
-
7/27/2019 Java chapter 15
167/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
se a %'anelas a dedicated dra$ing areain $hich the user candra$ " dragging the #ouse.
ight$eight $ing co#ponents that etend class %)omponent(such as %'anel) contain #ethod painto!ponent called $hen a light$eight $ing co#ponent is displa"ed
&verride this #ethod to specif" ho$ to dra$. Call the superclass version of paint)omponentas the first state#ent
in the od" of the overridden #ethod to ensure that the co#ponentdispla"s correctl".
-
7/27/2019 Java chapter 15
168/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
%)omponentsupport transparenc". 8o displa" a co#ponent correctl", the progra# #ust deter#ine $hether
the co#ponent is transparent.
8he code that deter#ines this is in superclass %)omponentspaint)omponenti#ple#entation.
=hen a co#ponent is transparent, paint)omponent$ill not clear its
acground =hen a co#ponent is opa*ue, paint)omponentclears the
co#ponents acground
8he transparenc" of a $ing light$eight co#ponent can e set $ith#ethod setOpa5ue(a falseargu#ent indicates that the co#ponentis transparent).
-
7/27/2019 Java chapter 15
169/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
170/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
171/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
172/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
173/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
174/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
175/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Class Point(pacage java.awt) represents anx-ycoordinate. =e use oDects of this class to store the coordinates of each #ouse
drag event.
Class G"aphi(sis used to dra$.
-ousevent#ethod getPointotains the 'oint$here the event occurred. Bethod "epaint(inherited fro# )omponent) indicates
that a )omponentshould e refreshed on the screen assoon as possile.
-
7/27/2019 Java chapter 15
176/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
177/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
6raphics#ethod illOvaldra$s a solid oval. 5our para#eters represent a rectangular area (called the ounding
o) in $hich the oval is displa"ed.
8he first t$o are the upper!left !coordinate and the upper!lefty-coordinate of the rectangular area.
8he last t$o represent the rectangular areas $idth and height.
Bethod fill&valdra$s the oval so it touches the #iddleof each side of the rectangular area.
-
7/27/2019 Java chapter 15
178/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
179/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
180/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
i i f f h dli
-
7/27/2019 Java chapter 15
181/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
e,Listenerinterface for handling e" events.
>e" events are generated $hen e"s on the e"oard are pressed and released. A e,Listener#ust define #ethods 1e%P"esse', 1e%)elease'and1e%&%pe' each receives a e,ventas its argu#ent
Class e,ventis a suclass of (nputvent. Bethod ke,'ressedis called in response to pressing an" e".
Bethod ke,4,pedis called in response to pressing an" e" that is not anaction e". Bethod ke,0eleasedis called $hen the e" is released after an"ke,'ressedor ke,4,pedevent.
-
7/27/2019 Java chapter 15
182/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
183/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
184/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
185/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
186/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
? i t t h dl ith th d ''/ Li t f
-
7/27/2019 Java chapter 15
187/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
?egisters e" event handlers $ith #ethod a''/e%Listene"fro#
class )omponent. e,vent#ethod get/e%o'egets the virtual e" codeof thepressed e".
e,ventcontains virtual e"!code constants that represents ever"e" on the e"oard.
alue returned " gete,)odecan e passed to static
e,vent#ethod get/e%&extto get a string containing the na#eof the e" that $as pressed.
e,vent#ethod get/e%ha"($hich returns a char) gets thenicode value of the character t"ped.
e,vent#ethod isA(tion/e%deter#ines $hether the e" in theevent $as an action e".
B th d tM 'ii d t i h th difi
-
7/27/2019 Java chapter 15
188/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Bethod getMo'iie"sdeter#ines $hether an" #odifier
e"s (such as Shift, Alt andCtrl) $ere pressed $hen the e"event occurred. ?esult can e passed to statice,vent#ethodget/e%Mo'iie"s&extto get a string containing the na#es ofthe pressed #odifier e"s.
(nputvent#ethods isAltDown, isont"olDown,isMetaDownand isShitDowneach return abooleanindicating $hether the particular e" $as pressedduring the e" event.
t GI t i t i
-
7/27/2019 Java chapter 15
189/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
a"out #anagersarrange GI co#ponents in a container
for presentation purposes Can use for asic la"out capailities Enale "ou to concentrate on the asic loo!and!feel-the
la"out #anager handles the la"out details. a"out #anagers i#ple#ent interface La%outManage"
(in pacage java.awt). )ontainers setLa,out#ethod taes an oDect that
i#ple#ents the La,out-anagerinterface as anargu#ent.
-
7/27/2019 Java chapter 15
190/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
8here are three $a"s for "ou to arrange co#ponents in
a GI3
)bsolute positioning*reatest level of control.
Set )ontainer+s layout to null.
Specify the absolute position of each *,$ component withrespect to the upper- left corner of the )ontainerby using)omponentmethods setSi;eand setLocationorset5ounds.
ust specify each *,$ component+s si#e.
t
-
7/27/2019 Java chapter 15
191/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
ayout managers
Simpler and faster than absolute positioning.
ose some control over the si#e and the precise positioning of*,$ components.
/isual programming in an $01
,se tools that make it easy to create *,$s.)llows you to drag and drop *,$ components from a tool bo
onto a design area.
3ou can then position% si#e and align *,$ components as youlike.
-
7/27/2019 Java chapter 15
192/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
193/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
194/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
l L t i th i l t l t
-
7/27/2019 Java chapter 15
195/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
lowLa,outis the si#plest la"out #anager. GI co#ponents placed fro# left to right in the order in
$hich the" are added to the container.
=hen the edge of the container is reached, co#ponents
continue to displa" on the net line.
lowLa,outallo$s GI co#ponents to e left aligned,centered (the default) and right aligned.
-
7/27/2019 Java chapter 15
196/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
197/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
198/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
199/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
200/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
201/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
202/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
lowLa,out #ethod setAlign!ent changes the
-
7/27/2019 Java chapter 15
203/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
lowLa,out#ethod setAlign!entchanges thealign#ent for the lowLa,out. $lowLa%out.LE$&
$lowLa%out.EN&E)
$lowLa%out.)IG*&
La,out-anagerinterface #ethod la%outontaine"($hich is inherited " all la"out #anagers) specifies that a
container should e rearranged ased on the adDusted la"out.
5orderLa,out
-
7/27/2019 Java chapter 15
204/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
,
the default la"out #anager for a %frame arranges co#ponents into five regions3 N&04
-
7/27/2019 Java chapter 15
205/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
206/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
207/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
208/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
209/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
210/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
5orderLa,out constructor argu#ents specif" the
-
7/27/2019 Java chapter 15
211/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
5orderLa,outconstructor argu#ents specif" thenu#er of piels et$een co#ponents that are
arranged horiontall" (horiontal gap space) and
et$een co#ponents that are arranged verticall"
(vertical gap space), respectivel".
8he default is one piel of gap space horiontall" and
verticall".
-
7/27/2019 Java chapter 15
212/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
213/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
G"i'La%outdivides the container into a grid of ro$s and
-
7/27/2019 Java chapter 15
214/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
g
colu#ns. I#ple#ents interface La,out-anager. Ever" )omponenthas the sa#e $idth and height. Co#ponents are added starting at the top!left cell of the grid and
proceeding left to right until the ro$ is full. 8hen the processcontinues left to right on the net ro$ of the grid, and so on.
)ontainer#ethod vali'atereco#putes thecontainers la"out ased on the current la"out #anager andthe current set of displa"ed GI co#ponents.
-
7/27/2019 Java chapter 15
215/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
216/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
217/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
218/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
Co#ple GIs re*uire that each co#ponent e placed in an
-
7/27/2019 Java chapter 15
219/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
eact location. &ften consist of #ultiple panels, $ith each panels co#ponents arranged
in a specific la"out. Class %'aneletends %)omponentand %)omponent
etends class )ontainer, so ever" %'anelis a )ontainer. Ever" %'anel#a" have co#ponents, including other panels,
attached to it $ith )ontainer#ethod add. %'anelcan e used to create a #ore co#ple la"out in $hich
several co#ponents are in a specific area of another container.
-
7/27/2019 Java chapter 15
220/231
-
7/27/2019 Java chapter 15
221/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
222/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A J&extA"eaprovides an area for #anipulating
-
7/27/2019 Java chapter 15
223/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
p p g
#ultiple lines of tet. %4e"tAreais a suclass of %4e"t)omponent,
$hich declares co##on #ethods for %4e"tields,%4e"tAreas and several other tet!ased GIco#ponents.
-
7/27/2019 Java chapter 15
224/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
225/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
-
7/27/2019 Java chapter 15
226/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
A J&extA"eaprovides an area for #anipulating
-
7/27/2019 Java chapter 15
227/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
#ultiple lines of tet. %4e"tAreais a suclass of %4e"t)omponent.
-
7/27/2019 Java chapter 15
228/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
0oxis a suclass of )ontainerthat uses a 0oxLa%outto arrange the GIco#ponents horiontall" or verticall".
-
7/27/2019 Java chapter 15
229/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
p " " 5o"static#ethod ("eate*o"i+ontal0oxcreates a 5o"that arranges
co#ponents left to right in the order that the" are attached. %4e"tArea#ethod getSele(te'&ext(inherited fro#%4e"t)omponent) returns the selected tet fro# a %4e"tArea.
%4e"tArea#ethod set4e"tchanges the tet in a %4e"tArea. =hen tet reaches the right edge of a %4e"tAreathe tet can $rap to the
net line. ?eferred to as line $rapping.
+" default, %4e"tAreadoes not $rap lines.
ou can set the horiontal and vertical scrollar
-
7/27/2019 Java chapter 15
230/231
(C) 2010 Pearson Education, Inc. !! rig"ts re
policiesof a %Scroll'ane$hen its constructed. ou can also use %Scroll'ane#ethodsset*o"i+ontalS("oll0a"Poli(% and
set,e"ti(alS("oll0a"Poli(%to change the
scrollar policies.
Class %Scroll'anedeclares the constants%Scroll'ane.804()AL S)0&LL5A0 AL+AS
-
7/27/2019 Java chapter 15
231/231
%Scroll'ane.804()AL7S)0&LL5A07AL+AS%Scroll'ane.&N4AL7S)0&LL5A07AL+AS
to indicate that a scrollar should al$a"s appear, constants%Scroll'ane.804()AL7S)0&LL5A07AS7N$$%Scroll'ane.&N4AL7S)0&LL5A07AS7N$$
to indicate that a scrollar should appear onl" if necessar" (the defaults) andconstants%Scroll'ane.804()AL7S)0&LL5A07N80%Scroll'ane.&N4AL7S)0&LL5A07N80
to indicate that a scrollar should never appear. If polic" is set to &N4AL7S)0&LL5A07N80, a%4e"tAreaattached to the %Scroll'ane$ill auto#aticall" $raplines.