0% found this document useful (0 votes)
48 views231 pages

Java Chapter 15

This document provides information about graphical user interfaces (GUIs) in Java. It discusses GUI components, containers, frames, look and feel, and dialog boxes. Key points include: GUIs present a user-friendly interface for applications; Swing components provide cross-platform GUI development; containers organize components on screen; frames contain Swing components and provide basic window attributes; and dialog boxes interact with users through pop-up windows.

Uploaded by

Tony Yu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views231 pages

Java Chapter 15

This document provides information about graphical user interfaces (GUIs) in Java. It discusses GUI components, containers, frames, look and feel, and dialog boxes. Key points include: GUIs present a user-friendly interface for applications; Swing components provide cross-platform GUI development; containers organize components on screen; frames contain Swing components and provide basic window attributes; and dialog boxes interact with users through pop-up windows.

Uploaded by

Tony Yu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 231

Java How to Program, 8/e

(C) 2010 Pearson Education, Inc. All rights reserved.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A graphical user interface (G I) 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 *uic(l" and use the# #ore productivel".

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

+uilt fro# G I co#ponents.


,o#eti#es called controls or $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 G I design tools to specif" a co#ponent0s e1act si2e and location in a visual #anner ." using the #ouse. Generates the G I code for "ou. Greatl" si#plifies creating G Is, .ut each I/E has different capa.ilities and generates different code.
(C) 2010 Pearson Education, Inc.

!! rig"ts res

E1a#ple of a G I3 ,$ing,et4 application (5ig.617.1) title .ar at top contains the $indo$0s title. #enu .ar contains #enus (File and View). In the top!right region of the $indo$ is a set of .uttons
8"picall", users press .uttons to perfor# tas(s.

http://download.java.net/javadesktop/swingset3/Swing

In the GUI Components area of the $indo$ is a co#.o .o19


ser can clic( the do$n arro$ at the right side of the .o1 to select fro# a list of ite#s.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

:ava ,E ; update 10 <e$, elegant, cross!platfor# loo(!and!feel (no$n as <i#.us. =e0ve configured our s"ste#s to use <i#.us as the default loo(!and!feel.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8hree $a"s to use <i#.us3


Set it as the default for all Java applications that run on your computer. Set it as the look- and- feel w hen you launch an application by passing a command- line argument to the java command. Set it as the look- and- feel programatically in your application (Section25. !.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8o set <i#.us as the default for all :ava applications3

Create a te1t file na#ed swing.properties in the lib folder of .oth "our :/> installation folder and "our :?E installation folder. Place the follo$ing line of code in the file3
swing.defaultlaf= com.sun.java.swing.plaf.nimbus.NimbusLookAnd eel

5or #ore infor#ation on locating these installation folders visit

http://java.sun.com/javase/!/webnotes/install/inde".html

@<ote3 In addition to the standalone :?E, there is a :?E nested in "our :/>0s installation folder. If "ou are using an I/E that depends on the :/> (e.g., <et+eans), "ou #a" also need to place the swing.properties file in the nested jre folder0s lib folder.A

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8o select <i#.us on an application!."!application .asis3


Place the follo$ing co##and!line argu#ent after the java co##and and .efore the application0s na#e $hen "ou run the application3
#$swing.defaultlaf= com.sun.java.swing.plaf.nimbus. NimbusLookAnd eel

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Bost applications use $indo$s or dialog .o1es (also called dialogs) to interact $ith the user. JOptionPane (pac(age java".swing) provides pre.uilt dialog .o1es for input and output
/ispla"ed via static %&ption'ane #ethods.

5igure617.2 uses t$o input dialogs to o.tain integers fro# the user and a #essage dialog to displa" the su# of the integers the user enters.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

%&ption'ane static #ethod showInputDialog displa"s an input dialog, using the #ethod0s String argu#ent as a pro#pt.

8he user t"pes characters in the te1t field, then clic(s OK or presses the Enter (e" to su.#it the String to the progra#. Clic(ing OK dis#isses (hides) the dialog. Can input onl" Strings. 8"pical of #ost G I co#ponents. If the user clic(s Cancel, returns null. %&ption'ane dialog are dialog-the user cannot interact $ith the rest of the application $hile dialog is displa"ed.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Converting Strings to int Calues Bessage /ialogs

(nteger class0s static #ethod parse(nt converts its String argu#ent to an int value. %&ption'ane static #ethod showMessageDialog displa"s a #essage dialog. 8he first argu#ent helps deter#ine $here to position the dialog. 8he second argu#ent is the #essage to displa". 8he third argu#ent is the String that 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".
If null, the dialog .o1 is displa"ed at the center of "our screen.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Bessage /ialogs
A JOption-Pane.PLAIN_MESSAGE dialog does not displa" an icon to the left of the #essage.

%&ption'ane online docu#entation3

http://java.sun.com/javase/!/docs/api/java"/sw

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

,$ing G I co#ponents located in pac(age javax.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# G I develop#ent. :5C and :ava des(top technologies3 http://java.sun.com/javase/technologies/desktop/

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A.stract =indo$ 8ool(it (A=8) in pac(age java.awt is another set of G I co#ponents in :ava.
=hen a :ava application $ith an A=8 G I e1ecutes on different :ava platfor#s, the application0s G I co#ponents displa" differentl" on each platfor#.

8ogether, the appearance and the $a" in $hich the user interacts $ith the application are (no$n as that application0s loo(!and! feel. ,$ing G I co#ponents allo$ "ou to specif" a unifor# loo(! and!feel for "our application across all platfor#s or to use each platfor#0s custo# loo(!and!feel.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Bost ,$ing co#ponents are not tied to actual G I co#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 the local platfor#0s $indo$ing s"ste# to deter#ine their functionalit" and their loo(!and!feel. ,everal ,$ing co#ponents are heav"$eight co#ponents.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Class o!ponent (pac(age java.awt) declares #an" of the attri.utes and .ehaviors co##on to the G I co#ponents in pac(ages java.awt and java".swing. Bost G I co#ponents e1tend class )omponent directl" or indirectl". &nline docu#entation3

http://java.sun.com/javase/!/docs/api/java/awt *

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Class ontaine" (pac(age java.awt) is a su.class of )omponent. )omponents are attached to )ontainers so that the" can .e organi2ed and displa"ed on the screen. An" o.Dect that is a )ontainer can .e used to organi2e other )omponents in a G I. +ecause a )ontainer is a )omponent, "ou can place )ontainers in other )ontainers to help organi2e a G I. &nline docu#entation3

http://java.sun.com/javase/!/docs/api/java/awt/)onta *

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Class J o!ponent (pac(age java".swing) is a su.class of )ontainer. %)omponent is the superclass of all light$eight ,$ing co#ponents, all of $hich are also )ontainers.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

,o#e co##on light$eight co#ponent features supported ." %)omponent include3

pluggable look- and- feel Shortcut keys (called mnemonics! "ommon event- handling capabilities for components that initiate the same actions in an application. tool tips Support for accessibility Support for user- interface locali#ation

&nline docu#entation3

http://java.sun.com/javase/!/docs/api/java"#/s wing/%)omponent.html

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Bost $indo$s that can contain ,$ing G I co#ponents are instances of class % rame or a su.class of % rame. % rame is an indirect su.class of class java.awt.+indow Provides the .asic attri.utes and .ehaviors of a $indo$
a title .ar at the top .uttons to #ini#i2e, #a1i#i2e and close the $indo$

Bost of our e1a#ples $ill consist of t$o classes

a su.class of % rame that de#onstrates ne$ G I concepts an application class in $hich main creates and displa"s the application0s pri#ar" $indo$.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

In a large G I

/ifficult to identif" the purpose of ever" co#ponent. Provide te1t stating each co#ponent0s purpose.

,uch te1t is (no$n as a la.el and is created $ith class JLa#el-a su.class of %)omponent.
/ispla"s read!onl" te1t, an i#age, or .oth te1t and an i#age.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

% rame0s constructor uses its String argu#ent as the te1t in the $indo$0s title .ar. Bust attach each G I co#ponent to a container, such as a % rame. Eou t"picall" #ust decide $here to position each G I co#ponent.
>no$n as specif"ing the la"out of the G I co#ponents. :ava provides several la"out #anagers that can help "ou position co#ponents.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Ban" I/Es provide G I design tools in $hich "ou can specif" the e1act si2e and location of a co#ponent I/E generates the G I code for "ou Greatl" si#plifies G I creation 8o ensure that this .oo(0s e1a#ples can .e used $ith an" I/E, $e did not use an I/E to create the G I code =e use :ava0s la"out #anagers in our G I e1a#ples

(C) 2010 Pearson Education, Inc.

!! rig"ts res

$lowLa%out

G I 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 ne1t line. If the container is resi2ed, a lowLa,out reflo$s the co#ponents to acco##odate the ne$ $idth of the container, possi.l" $ith fe$er or #ore ro$s of G I co#ponents. argu#ent #ust .e an o.Dect of a class that i#ple#ents the La,out-anager interface (e.g., lowLa,out).

Bethod setLa%out is inherited fro# class )ontainer.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

%Label constructor can receive a String specif"ing the la.el0s te1t. Bethod set&ool&ip&ext (inherited ." %Label fro# %)omponent) specifies the tool tip that is displa"ed $hen the user positions the #ouse cursor over a %)omponent (such as a %Label). Eou attach a co#ponent to a container using the a'' #ethod, $hich is inherited indirectl" fro# class )ontainer.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Icons enhance the loo(!and!feel of an application and are also co##onl" used to indicate functionalit". An icon is nor#all" specified $ith an I(on argu#ent to a constructor or to the co#ponent0s setI(on #ethod. An (con is an o.Dect of an" class that i#ple#ents interface (con (pac(age java".swing). I!ageI(on (pac(age java".swing) supports several i#age for#ats, including Graphics Interchange 5or#at (GI5), Porta.le <et$or( Graphics (P<G) and :oint Photographic E1perts Group (:PEG).

(C) 2010 Pearson Education, Inc.

!! rig"ts res

get)lass./.get0esource.1bug2.png1/
Invo(es #ethod get lass (inherited indirectl" fro# class &bject) to retrieve a reference to the )lass o.Dect that represents the Label rame class declaration. <e1t, invo(es )lass #ethod get)esou"(e, $hich returns the location of the i#age as a ?). 8he (mage(con constructor uses the ?) to locate the i#age, then loads it into #e#or". 8he class loader (no$s $here each class it loads is located on dis(. Bethod get0esource uses the )lass o.Dect0s class loader to deter#ine the location of a resource, such as an i#age file.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A %Label can displa" an (con. %Label constructor can receive te1t and an (con.

8he last constructor argu#ent indicates the Dustification of the la.el0s contents. Interface Swing onstants (pac(age java".swing) declares a set of co##on integer constants (such as Swing)onstants.L3 4) that are used $ith #an" ,$ing co#ponents. +" default, the te1t appears to the right of the i#age $hen a la.el contains .oth te1t and an i#age. 8he hori2ontal and vertical align#ents of a %Label can .e set $ith #ethods set*o"i+ontalAlign!ent and set,e"ti(alAlign!ent, respectivel".

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Class %Label provides #ethods to change a la.el0s appearance after it has .een instantiated. Bethod set&ext sets the te1t displa"ed on the la.el. Bethod get&ext retrievesF the current te1t displa"ed on a la.el. Bethod setI(on specifies the (con to displa" on a la.el. Bethod getI(on retrievesF the current (con displa"ed on a la.el. Bethods set*o"i+ontal&extPosition and set,e"ti(al&extPosition specif" the te1t position in the la.el.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

+" default, closing a $indo$ si#pl" hides the $indo$. Calling #ethod setDe-ault loseOpe"ation (inherited fro# class % rame) $ith the argu#ent J$"a!e.E.I&_ON_ LOSE indicates that the progra# should ter#inate $hen the $indo$ is closed ." the user. Bethod setSi+e specifies the $idth and height of the $indo$ in pi1els. Bethod set,isi#le $ith the argu#ent true displa"s the $indo$ on the screen.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

G Is are event driven. =hen the user interacts $ith a G I 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.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

J&ext$iel's and JPasswo"'$iel's (pac(age java".swing). %4e"t ield e1tends class J&ext o!ponent (pac(age java".swing.te"t), $hich provides #an" features co##on to ,$ing0s te1t!.ased co#ponents. Class %'assword ield e1tends %4e"t ield and adds #ethods that are specific to processing pass$ords. %'assword ield sho$s that characters are .eing t"ped as the user enters the#, .ut hides the actual characters $ith an echo character.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

=hen the user t"pes data into a %4e"t ield or a %'assword ield, then presses Enter, an event occurs. Eou can t"pe onl" in the te1t field that is %in focus.' A co#ponent receives the focus $hen the user clic(s the co#ponent.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

+efore an application can respond to an event for a particular G I co#ponent, "ou #ust perfor# several coding steps3
"reate a class that represents the event handler. $mplement an appropriate interface% know n as an eventlistener interface% in the class from St ep 1. $ndicate that an ob&ect of the class from Steps ' and 2 should be notified w hen the event occurs. (his is know n as registering the event handler.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

All the classes discussed so far $ere so!called top!level classes-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 static or non!static. <on!static nested classes are called inner classes and are fre*uentl" used to i#ple#ent event handlers.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

+efore an o.Dect of an inner class can .e created, there #ust first .e an o.Dect of the top!level class that contains the inner class. 8his is re*uired .ecause an inner!class o.Dect i#plicitl" has a reference to an o.Dect of its top!level class. 8here is also a special relationship .et$een these o.Dects-the inner!class o.Dect is allo$ed to directl" access all the varia.les and #ethods of the outer class. A nested class that is static does not re*uire an o.Dect of its top!level class and does not i#plicitl" have a reference to an o.Dect of the top!level class.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Inner classes can .e declared public, protected or private. ,ince event handlers tend to .e specific to the application in $hich the" are defined, the" are often i#ple#ented as private inner classes.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

G I co#ponents can generate #an" events in response to user interactions. Each event is represented ." a class and can .e processed onl" ." the appropriate t"pe of event handler. <or#all", a co#ponent0s supported events are descri.ed in the :ava API docu#entation for that co#ponent0s class and its superclasses.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

=hen the user presses Enter in a %4e"t ield or %'assword ield, an A(tionEvent (pac(age java.awt.event) occurs. Processed ." an o.Dect that i#ple#ents the interface A(tionListene" (pac(age java.awt.event). 8o handle Action3vents, a class #ust i#ple#ent interface ActionListener and declare #ethod action'erformed.

8his #ethod specifies the tas(s to perfor# $hen an Action3vent occurs.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Bust register an o.Dect as the event handler for each te1t field. a''A(tionListene" registers an ActionListener o.Dect to handle Action3vents. After an event handler is registered the o.Dect listens for events.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8he G I co#ponent $ith $hich the user interacts is the event source. Action3vent #ethod getSource (inherited fro# class 3vent&bject) returns a reference to the event source. Action3vent #ethod getA(tion o!!an' o.tains the te1t the user t"ped in the te1t field that generated the event. %'assword ield #ethod getPasswo"' returns the pass$ord0s characters as an arra" of t"pe char.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

5igure617.11 illustrates a hierarch" containing #an" event classes fro# the pac(age java.awt.event. sed $ith .oth A=8 and ,$ing co#ponents. Additional event t"pes that are specific to ,$ing G I co#ponents are declared in pac(age javax.swing.event.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

/elegation event #odel-an event0s processing is delegated to an o.Dect (the event listener) in the application. 5or each event!o.Dect 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 pac(age java.awt.event, and so#e of the# are sho$n in 5ig.617.12.

Additional event!listener t"pes that are specific to ,$ing co#ponents are declared in pac(age java".swing.event.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Each event!listener interface specifies one or #ore event!handling #ethods that #ust .e declared in the class that i#ple#ents the interface. =hen an event occurs, the G I co#ponent $ith $hich the user interacted notifies its registered listeners ." calling each listener0s appropriate event!handling #ethod.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Go$ the event!handling #echanis# $or(s3 Ever" %)omponent has a varia.le listenerList that refers to an EventListene"List (pac(age java".swing.event). Baintains references to registered listeners in the listenerList. =hen a listener is registered, a ne$ entr" is placed in the co#ponent0s listenerList. Ever" entr" also includes the listener0s t"pe.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Go$ does the G I co#ponent (no$ to call action'erformed rather than another #ethodH

Ever" G I co#ponent supports several event t"pes, including #ouse events, (e" events and others. =hen an event occurs, the event is dispatched onl" to the event listeners of the appropriate t"pe. /ispatching is si#pl" the process ." $hich the G I co#ponent calls an event!handling #ethod on each of its listeners that are registered for the event t"pe that occurred.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Each event t"pe has one or #ore corresponding event!listener interfaces.


Action3vents are handled ." ActionListeners MouseEvents are handled ." MouseListene"s and MouseMotionListene"s /e%Events are handled ." /e%Listene"s

=hen an event occurs, the G I co#ponent receives (fro# the :CB) 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 each listener o.Dect.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

5or an Action3vent, the event is dispatched to ever" registered ActionListener0s action'erformed #ethod. 5or a -ouse#3vent, the event is dispatched to ever" registered -ouseListener or -ouse-otionListener, depending on the #ouse event that occurs.
8he -ouse3vent0s event I/ deter#ines $hich of the several #ouse event!handling #ethods are called.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A .utton is a co#ponent the user clic(s to trigger a specific action. ,everal t"pes of .uttons
co##and .uttons chec(.o1es toggle .uttons radio .uttons

+utton t"pes are su.classes of A#st"a(t0utton (pac(age java".swing), $hich declares the co##on features of ,$ing .uttons.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A co##and .utton generates an Action3vent $hen the user clic(s it. Co##and .uttons are created $ith class J0utton. 8he te1t on the face of a %5utton is called a .utton la.el.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A %5utton can displa" an (con. A %5utton can also have a rollover I(on

displa"ed $hen the user positions the #ouse over the %5utton. 8he icon on the %5utton changes as the #ouse #oves in and out of the %5utton0s area on the screen.

Abstract5utton #ethod set)ollove"I(on specifies the i#age displa"ed on the %5utton $hen the user positions the #ouse over it.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

%5uttons, li(e %4e"t ields, generate Action3vents that can .e processed ." an" ActionListener o.Dect.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8hree t"pes of state .uttons-J&oggle0utton, J he(10ox and J)a'io0utton-that have onIoff or trueIfalse values. Classes %)heck5o" and %0adio5utton are su.classes of %4oggle5utton. %0adio5uttons are grouped together and are #utuall" e1clusive-onl" one in the group can .e selected at an" ti#e

(C) 2010 Pearson Education, Inc.

!! rig"ts res

%4e"t ield #ethod set$ont (inherited ." %4e"t ield indirectl" fro# class )omponent) sets the font of the %4e"t ield to a ne$ $ont (pac(age java.awt). String passed to the %)heck5o" constructor is the chec(.o1 la.el that appears to the right of the %)heck5o" ." default. =hen the user clic(s a %)heck5o", an Ite!Event occurs. An (temListener is registered $ith #ethod a''Ite!Listene". %)heck5o" #ethod isSele(te' returns true if a %)heck5o" is selected.
Gandled ." an Ite!Listene" o.Dect, $hich #ust i#ple#ent #ethod ite!State hange'.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

?adio .uttons (declared $ith class %0adio5utton) are si#ilar to chec(.o1es in that the" have t$o states-selected and not selected (also called deselected). ?adio .uttons nor#all" appear as a group in $hich onl" one .utton can .e selected at a ti#e. ,electing a different radio .utton forces all others to .e deselected. sed to represent #utuall" e1clusive options. 8he logical relationship .et$een radio .uttons is #aintained ." a 0uttonG"oup o.Dect (pac(age java".swing), $hich organi2es a group of .uttons and is not itself displa"ed in a user interface.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

5utton6roup #ethod a'' associates a %0adio5utton $ith the group. If #ore than one selected %0adio5utton o.Dect is added to the group, the selected one that $as added first $ill .e selected $hen the G I is displa"ed. %0adio5uttons, li(e %)heck5o"es, generate (tem3vents $hen the" are clic(ed.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A co#.o .o1 (or drop!do$n list) ena.les the user to select one ite# fro# a list. Co#.o .o1es are i#ple#ented $ith class J o!#o0ox, $hich e1tends class %)omponent. %)ombo5o"es generate (tem3vents.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8he first ite# added to a %)ombo5o" appears as the currentl" selected ite# $hen the %)ombo5o" is displa"ed. &ther ite#s are selected ." clic(ing the %)ombo5o", then selecting an ite# fro# the list that appears. %)ombo5o" #ethod setMaxi!u!)ow ount sets the #a1i#u# nu#.er of ele#ents that are displa"ed $hen the user clic(s the %)ombo5o". If there are additional ite#s, the %)ombo5o" provides a scroll.ar that allo$s the user to scroll through all the ele#ents in the list.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

An anon"#ous inner class is an inner class that is declared $ithout a na#e and t"picall" appears inside a #ethod declaration. As $ith other inner classes, an anon"#ous inner class can access its top!level class0s #e#.ers. An anon"#ous inner class has li#ited access to the local varia.les of the #ethod in $hich it0s declared. ,ince an anon"#ous inner class has no na#e, one o.Dect of the anon"#ous inner class #ust .e created at the point $here the class is declared.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

%)ombo5o" #ethod getSele(te'In'ex returns the inde1 of the selected ite#. 5or each ite# selected fro# a %)ombo5o", another ite# is first deselected-so t$o (tem3vents occur $hen an ite# is selected. (tem3vent #ethod getState hange returns the t"pe of state change. (tem3vent.S3L3)43$ indicates that an ite# $as selected.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

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" e1tends class %)omponent.ss ,upports single!selection lists (onl" one ite# to .e selected at a ti#e) and #ultiple!selection lists (an" nu#.er of ite#s to .e selected). %Lists generate ListSele(tionEvents in single!selection lists.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

set,isi#le)ow ount specifies the nu#.er of ite#s visi.le in the list. setSele(tionMo'e specifies the list0s selection #ode. Class ListSele(tionMo'el (of pac(age 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 several contiguous ite#s) M2L&IPLE_IN&E),AL_SELE &ION (does not restrict the ite#s that can .e selected).

(C) 2010 Pearson Education, Inc.

!! rig"ts res

nli(e a %)ombo5o", a %List does not provide a scroll.ar if there are #ore ite#s in the list than the nu#.er of visi.le ro$s.
A JS("ollPane o.Dect is used to provide the scrolling capa.ilit".

a''ListSele(tionListene" registers a ListSele(tionListene" (pac(age java".swing.event) as the listener for a%List0s selection events.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Each % rame actuall" consists of three la"ers-the .ac(ground, the content pane and the glass pane. 8he content pane appears in front of the .ac(ground and is $here the G I co#ponents in the % rame are displa"ed. 8he glass pane is displa"s tool tips and other ite#s that should appear in front of the G I co#ponents on the screen. 8he content pane co#pletel" hides the .ac(ground of the % rame. 8o change the .ac(ground color .ehind the G I co#ponents, "ou #ust change the content pane0s .ac(ground color. Bethod get)ontent'ane returns a reference to the % rame0s content pane (an o.Dect of class )ontainer). List #ethod getSele(te'In'ex returns the selected ite#0s inde1.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A #ultiple!selection list ena.les the user to select #an" ite#s fro# a %List. A S(N6L37(N4308AL7S3L3)4(&N list allo$s selecting a contiguous range of ite#s.

8o do so, clic( the first ite#, then press and hold the Shift (e" $hile clic(ing the last ite# in the range.

A -9L4('L37(N4308AL7S3L3)4(&N list (the default) allo$s continuous range selection as descri.ed for a S(N6L37(N4308AL7S3L3)4(&N list and allo$s #iscellaneous ite#s to .e selected ." pressing and holding the Ctrl (e" $hile clic(ing each ite# to select.
8o deselect an ite#, press and hold the Ctrl (e" $hile clic(ing the ite# a second ti#e.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

If a %List does not contain ite#s it $ill not dipla" in a lowLa,out.

use %List #ethods set$ixe' ell3i'th and set$ixe' ell*eight to set the ite# $idth and height An event generated ." another G I co#ponent ((no$n as an e1ternal event) specifies $hen the #ultiple selections in a %List should .e processed.

8here are no events to indicate that a user has #ade #ultiple selections in a #ultiple!selection list. Bethod setListData sets the ite#s displa"ed in a %List. -ethod getSele(te',alues returns an arra" of &bjects representing the selected ite#s in a %List.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

MouseListene" and MouseMotionListene" event! listener interfaces for handling #ouse events.
An" G I co#ponent

Pac(age java".swing.event contains interface MouseInputListene", $hich e1tends interfaces -ouseListener and -ouse-otionListener to create a single interface containing all the #ethods. -ouseListener and -ouse-otionListener #ethods are called $hen the #ouse interacts $ith a )omponent if appropriate event!listener o.Dects are registered for that )omponent.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Each #ouse event!handling #ethod receives a MouseEvent o.Dect that contains infor#ation a.out the #ouse event that occurred, including the x- and "!coordinates of the location $here the event occurred. Coordinates are #easured fro# the upper!left corner of the G I co#ponent on $hich the event occurred. 8he 1!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# 3vent0s superclass) ena.le "ou to deter#ine $hich #ouse .utton the user clic(ed.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Interface Mouse3heelListene" ena.les applications to respond to the rotation of a #ouse $heel. Bethod !ouse3heelMove' receives a Mouse3heelEvent as its argu#ent. Class -ouse+heel3vent (a su.class of -ouse# 3vent) contains #ethods that ena.le the event handler to o.tain infor#ation a.out the a#ount of $heel rotation.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

0o"'e"La%out arranges co#ponents into five regions3 NO)&*, SO2&*, EAS&, 3ES& and EN&E). 5orderLa,out si2es the co#ponent in the )3N430 to use all availa.le space that is not occupied Bethods a''MouseListene" and a''MouseMotionListene" register -ouseListeners and -ouse-otionListeners, respectivel". -ouse3vent #ethods get. and get4 return the x- and y! coordinates of the #ouse at the ti#e the event occurred.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Ban" event!listener interfaces contain #ultiple #ethods. An adapter class i#ple#ents an interface and provides a default i#ple#entation ($ith an e#pt" #ethod .od") of each #ethod in the interface. Eou e1tend an adapter class to inherit the default i#ple#entation of ever" #ethod and override onl" the #ethod(s) "ou need for event handling.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A #ouse can have one, t$o or three .uttons. Class -ouse3vent inherits several #ethods fro# (nput3vent that can distinguish a#ong #ouse .uttons or #i#ic a #ulti.utton #ouse $ith a co#.ined (e"stro(e and #ouse!.utton clic(. :ava assu#es that ever" #ouse contains a left #ouse .utton.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

In the case of a one! or t$o!.utton #ouse, a :ava application assu#es that the center #ouse .utton is clic(ed if the user holds do$n the Alt (e" and clic(s the left #ouse .utton on a t$o! .utton #ouse or the onl" #ouse .utton on a one!.utton #ouse. In the case of a one!.utton #ouse, a :ava application assu#es that the right #ouse .utton is clic(ed if the user holds do$n the Meta (e" (so#eti#es called the Co##and (e" or the %Apple' (e" on a Bac) and clic(s the #ouse .utton.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8he nu#.er of consecutive #ouse clic(s is returned ." -ouse3vent #ethod get li(1 ount. Bethods isMetaDown and isAltDown deter#ine $hich #ouse .utton the user clic(ed.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

se a %'anel as a dedicated dra$ing area in $hich the user can dra$ ." dragging the #ouse. )ight$eight ,$ing co#ponents that e1tend class %)omponent (such as %'anel) contain #ethod paint o!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)omponent as the first state#ent in the .od" of the overridden #ethod to ensure that the co#ponent displa"s correctl".

(C) 2010 Pearson Education, Inc.

!! rig"ts res

%)omponent support 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 %)omponent0s paint)omponent i#ple#entation. =hen a co#ponent is transparent, paint)omponent $ill not clear its .ac(ground =hen a co#ponent is opa*ue, paint)omponent clears the co#ponent0s .ac(ground 8he transparenc" of a ,$ing light$eight co#ponent can .e set $ith #ethod setOpa5ue (a false argu#ent indicates that the co#ponent is transparent).

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Class Point (pac(age java.awt) represents an x-y coordinate.

=e use o.Dects of this class to store the coordinates of each #ouse drag event.

Class G"aphi(s is used to dra$. -ouse3vent #ethod getPoint o.tains the 'oint $here the event occurred. Bethod "epaint (inherited fro# )omponent) indicates that a )omponent should .e refreshed on the screen as soon as possi.le.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

6raphics #ethod -illOval dra$s a solid oval.

5our para#eters represent a rectangular area (called the .ounding .o1) in $hich the oval is displa"ed. 8he first t$o are the upper!left 1!coordinate and the upper!left ycoordinate of the rectangular area. 8he last t$o represent the rectangular area0s $idth and height.

Bethod fill&val dra$s the oval so it touches the #iddle of each side of the rectangular area.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

:e,Listener interface 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' and 1e%&%pe'
each receives a :e,3vent as its argu#ent

Class :e,3vent is a su.class of (nput3vent. Bethod ke,'ressed is called in response to pressing an" (e". Bethod ke,4,ped is called in response to pressing an" (e" that is not an action (e". Bethod ke,0eleased is called $hen the (e" is released after an" ke,'ressed or ke,4,ped event.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

?egisters (e" event handlers $ith #ethod a''/e%Listene" fro# class )omponent. :e,3vent #ethod get/e% o'e gets the virtual (e" code of the pressed (e". :e,3vent contains virtual (e"!code constants that represents ever" (e" on the (e".oard. Calue returned ." get:e,)ode can .e passed to static :e,3vent #ethod get/e%&ext to get a string containing the na#e of the (e" that $as pressed. :e,3vent #ethod get/e% ha" ($hich returns a char) gets the nicode value of the character t"ped. :e,3vent #ethod isA(tion/e% deter#ines $hether the (e" in the event $as an action (e".

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Bethod getMo'i-ie"s deter#ines $hether an" #odifier (e"s (such as Shift, Alt and Ctrl) $ere pressed $hen the (e" event occurred.
?esult can .e passed to static :e,3vent #ethod get/e%Mo'i-ie"s&ext to get a string containing the na#es of the pressed #odifier (e"s.

(nput3vent #ethods isAltDown, is ont"olDown, isMetaDown and isShi-tDown each return a boolean indicating $hether the particular (e" $as pressed during the (e" event.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

)a"out #anagers arrange G I co#ponents in a container for presentation purposes Can use for .asic la"out capa.ilities Ena.le "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 pac(age java.awt). )ontainer0s setLa,out #ethod ta(es an o.Dect that i#ple#ents the La,out-anager interface as an argu#ent.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

8here are three $a"s for "ou to arrange co#ponents in a G I3


) bsolute positioning
* reatest level of control. Set )ontainer+s layout to null. Specify the absolute position of each * ,$ component w ith respect to the upper- left corner of the )ontainer by using )omponent methods setSi;e and setLocation or set5ounds. ust specify each * ,$ component+s si#e.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

.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 $0 1
,se tools that make it easy to create * ,$s. ) llow s you to drag and drop * ,$ components from a tool bo2 onto a design area. 3ou can then position% si#e and align * ,$ components as you like.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

lowLa,out is the si#plest la"out #anager. G I 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 ne1t line. lowLa,out allo$s G I co#ponents to .e left aligned, centered (the default) and right aligned.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

lowLa,out #ethod setAlign!ent changes the align#ent for the lowLa,out.


$lowLa%out.LE$& $lowLa%out. EN&E) $lowLa%out.)IG*&

La,out-anager interface #ethod la%out ontaine" ($hich is inherited ." all la"out #anagers) specifies that a container should .e rearranged .ased on the adDusted la"out.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

5orderLa,out

the default la"out #anager for a %frame arranges co#ponents into five regions3 N&04<, S&94<, 3AS4, +3S4 and )3N430. N&04< corresponds to the top of the container.

5orderLa,out i#ple#ents interface La%outManage"6 (a su.interface of La,out-anager that adds several #ethods for enhanced la"out processing). 5orderLa,out li#its a )ontainer to at #ost five co#ponentsone in each region.
8he co#ponent placed in each region can .e a container to $hich other co#ponents are attached.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

5orderLa,out constructor argu#ents specif" the nu#.er of pi1els .et$een co#ponents that are arranged hori2ontall" (hori2ontal gap space) and .et$een co#ponents that are arranged verticall" (vertical gap space), respectivel".
8he default is one pi1el of gap space hori2ontall" and verticall".

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

G"i'La%out divides the container into a grid of ro$s and colu#ns.


I#ple#ents interface La,out-anager. Ever" )omponent has 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 process continues left to right on the ne1t ro$ of the grid, and so on.

)ontainer #ethod vali'ate reco#putes the container0s la"out .ased on the current la"out #anager and the current set of displa"ed G I co#ponents.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Co#ple1 G Is re*uire that each co#ponent .e placed in an e1act location.

&ften consist of #ultiple panels, $ith each panel0s co#ponents arranged in a specific la"out.

Class %'anel e1tends %)omponent and %)omponent e1tends class )ontainer, so ever" %'anel is a )ontainer. Ever" %'anel #a" have co#ponents, including other panels, attached to it $ith )ontainer #ethod add. %'anel can .e used to create a #ore co#ple1 la"out in $hich several co#ponents are in a specific area of another container.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A J&extA"ea provides an area for #anipulating #ultiple lines of te1t. %4e"tArea is a su.class of %4e"t)omponent, $hich declares co##on #ethods for %4e"t ields, %4e"tAreas and several other te1t!.ased G I co#ponents.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

A J&extA"ea provides an area for #anipulating #ultiple lines of te1t. %4e"tArea is a su.class of %4e"t)omponent.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

(C) 2010 Pearson Education, Inc.

!! rig"ts res

0ox is a su.class of )ontainer that uses a 0oxLa%out to arrange the G I co#ponents hori2ontall" or verticall". 5o" static #ethod ("eate*o"i+ontal0ox creates 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 te1t fro# a %4e"tArea. %4e"tArea #ethod set4e"t changes the te1t in a %4e"tArea. =hen te1t reaches the right edge of a %4e"tArea the te1t can $rap to the ne1t line.
?eferred to as line $rapping. +" default, %4e"tArea does not $rap lines.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Eou can set the hori2ontal and vertical scroll.ar policies of a %Scroll'ane $hen it0s constructed. Eou can also use %Scroll'ane #ethods set*o"i+ontalS("oll0a"Poli(% and set,e"ti(alS("oll0a"Poli(% to change the scroll.ar policies.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

Class %Scroll'ane declares the constants


to indicate that a scroll.ar should al$a"s appear, constants to indicate that a scroll.ar should appear onl" if necessar" (the defaults) and constants to indicate that a scroll.ar should never appear.
%Scroll'ane.8304()AL7S)0&LL5A07N3830 %Scroll'ane.<&0(>&N4AL7S)0&LL5A07N3830 %Scroll'ane.8304()AL7S)0&LL5A07AS7N33$3$ %Scroll'ane.<&0(>&N4AL7S)0&LL5A07AS7N33$3$ %Scroll'ane.8304()AL7S)0&LL5A07AL+A=S %Scroll'ane.<&0(>&N4AL7S)0&LL5A07AL+A=S

If polic" is set to <&0(>&N4AL7S)0&LL5A07N3830, a %4e"tArea attached to the %Scroll'ane $ill auto#aticall" $rap lines.

(C) 2010 Pearson Education, Inc.

!! rig"ts res

You might also like