Advanced Programming - Chapter 6.ppt
-
Upload
anonymous-r71gx2k -
Category
Documents
-
view
213 -
download
0
Transcript of Advanced Programming - Chapter 6.ppt
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 1/64
Chapter Six
GUI
1
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 2/64
GUI• Graphical User Interface ("Goo-ee")
• Pictoral interface to a program – Distinctie "loo!" an "feel"
• Different applications #ith consistent GUIsimproe pro$ctiit%
• &xample' – etscape Comm$nicator
– en$ *ar+ text fiel+ la*el
• GUIs *$ilt from components• Component' o*,ect #ith #hich $ser interacts
• &xamples' a*els+ .ext fiels+ /$ttons+Chec!*oxes
0
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 3/64
GUI
• .he graphical $ser interface allo#s $s tointeract #ith o$r programs thro$gh mo$semoements+ *$tton clic!s+ !e% presses+
an so on
• 2o$r 3ino#s or acintosh operating
s%stem proies %o$ #ith a GUI so %o$on4t hae to remem*er all sorts ofinstr$ctions to t%pe at the comman line
5
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 4/64
GUI
• .here are three *asic things that are necessar%
to eelop a graphical $ser interface
– The GUI components sets
• form the interface• *$ttons+ la*els+ chec! *oxes+ etc
– Component arrangement
• the scheme #here*% the UI components are arrange to
create the interface
– Response to user requests
• the act of associating actions to $ser re6$ests+ !no#n as
4eents4
7
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 5/64
Implementing GUIs in 8aa• .he 8aa 9o$nation Classes (89C)
&ncompass a gro$p of feat$res forconstr$cting graphical $ser interfaces (GUI)
• encompassing the follo#ing :PIs'
– Abstract Window Toolkit (AWT)' natie GUIcomponents
– wing' light#eight GUI components
– !"' renering t#o-imensional shapes+ text+ animages
– Accessibilit#' allo#ing compati*ilit% #ith+ for
example+ screen reaers an screen magnifiers;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 6/64
Abstract Window Toolkit
(AWT)• Proies *asic UI components'
– /$ttons+ lists+ men$s+ textfiels+ etc
– &ent hanling mechanism
– Clip*oar an ata transfer
– Image manip$lation
– 9ont manip$lation
– Graphics
• Platform inepenence is achiee
thro$gh peers+ or natie GUI components
<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 7/64
Using :3. Components• :3. is 8aa=s original set of classes for *$iling
GUIs – Uses peer components of the >S? hea%#eight
– ot tr$l% porta*le' loo!s ifferent an la%s o$t
inconsistentl% on ifferent >Ss
• D$e to >S=s $nerl%ing ispla% management s%stem
• &er% GUI component is a s$*class of the :3.
Component class (except for men$s)
•Controls allo# simple $ser inp$t• a*els ispla% text
• Containers arrange other components
• Use Canases for c$stom components@
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 8/64
Components and Containers• .he :3. set is str$ct$re in a class
hierarch%• eer% UI component is a escenant of
the Component class
• .his contains the er% *asic f$nctionalit%common to all components
A
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 9/64
:3. Components
primitiecontainer
B
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 10/64
Peers an Platform Inepenence• .h$s an :3. men$ on the Solaris platform+
for example+ act$all% creates a otif men$o*,ect as its peer
• UI components that hae peers are calle
heavyweight because – they are rendered in their own (opaque) windows
and thus are expensive to use,
– they must be rectangular and cannot havetransparent backgrounds, and
– they are not amenable to being subclassed
1
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 11/64
Using Peers
• : 8aa program creates an ispla%s an :3. component+
• #hich creates an ispla%s a natiecomponent+ or peer
• /% $sing peers• Platform controls component appearance
• Inconsistencies in implementations – -
JavaPr ogram
JavaAWT
NativeWindowSystemPeers
11
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 12/64
ight#eight Components• :3. 11 intro$ce the notion of lightweight
components #hich' – are containe #ithin a hea%#eight component4s #ino#
– o not hae peers
– are renere in their container4s #ino# rather than one
of their o#n – o not inc$r performance penalties an can hae
transparent *ac!gro$ns
• :lmost all S#ing components are light#eight onesthat exten either
• java.awt.Component or java.awt.Container
10
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 13/64
3hat is S#ing
• is 8aa4s graphical $ser interface li*rar%• :*stract 3ino# .ool!it (:3.) E0• 1F P$re 8aa
– e6$ires 8DH 110 or higher • Part of 89C – Components – e# high-leel components – Pl$gga*le oo! 9eel
• 2o$ US. import the follo#ing pac!ages'
import java.awt.*;import javax.swing.*;
15
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 14/64
S#ing Philosoph%
• icher Component Set
– eplaces :3. Component Set
– :s more complex components
• S#ing Components 8aa-/ase
– If pro*lems+ same pro*lems eer%#here
17
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 15/64
.he S#ing Containment Jierarch%
• t%picall% has at least'
– a top$le%el container& the root container thathols eer%thing together eg 8:pplet+89rame+ 3ino#
– an intermediate container& a container tosimplif% the positioning of atomic componentseg 8Panel+ 8ScrollPane
– an atomic component& a self-s$fficientcomponent not holing other components eg8/$tton+ 8a*el
1;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 16/64
• a Container is a special t%pe of component
• %o$ can a other components &xample+
89rame+ 8:pplet+ 8Panel etc
• :ll containers hae methos to a an
remoe components
Components and Containers
1<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 17/64
GUI Class Jierarch% (S#ing)
Dimension
Font
FontMetrics
Component
Graphics
Object Color
Container
Panel Applet
Frame
Dialog
Window
JComponent
JApplet
JFrame
JDialog
Swing Components
in te java!"swing pac#age
$igtweigt
%eavyweigt
Classes in te java"awt pac#age
&
LayoutManager
'
1@
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 18/64
Description of Classes
• >*,ect' :ll classes $ltimatel% erie from >*,ect+
th$s this class is at the top of the tree
• Component' represents an o*,ect that has a
is$al representation that can *e sho#n on-
screen an that can interact #ith $sers .his class
efines some *asic methos that are aaila*le to
all S#ing classes
1A
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 19/64
Description of Classes
(Cont=)
• Container' *$ils on the *asic is$al capa*ilities
of the Component class *% aing the a*ilit% to
hol other containers
• 3ino#' a specialiKe t%pe of container o*,ect
that has a *orer+ a title *ar+ *$ttons that
minimiKe+ maximiKe+ an close the #ino#+ an
that can *e repositione an possi*l% eenresiKe *% the $ser
1B
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 20/64
Description of Classes
(Cont=)
• 9rame' a t%pe of 3ino# that seres as the *asis
for 8aa GUI applications 9rame is an :3. class
that has *een improe $pon *% the 89rame
class
• 89rame' the S#ing ersion of the oler 9rame
class ost of the S#ing applications incl$e at
least one 89rame o*,ect• 8Component' is the *asis for all other S#ing
components except for frames
0
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 21/64
Description of Classes
(Cont=)
• 8Panel' $se to organiKe an control the
la%o$t of other components s$ch as la*els+
*$ttons+ text fiels+ etc In most S#ing
applications+ one or more panels are ae
to a frame .hen+ #hen the frame is
ispla%e+ the components that #ere ae
to its panels are mae isi*le• 8a*el' creates a la*el that ispla%s a simple
text al$e
01
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 22/64
:3. s S#ing• S#ing oes not replace the :3.? it is *$ilt on
top of it• :ll 1 :3. components are heavyweight;
corresponding Swing components are
lightweight • Swing component names begin with ``J!
– Component ("#$) vs% JComponent (Swing)
– Button ("#$) vs% JButton (Swing)
• "lways use Swing components; however,
since Swing is built on top o& "#$, you will
need to know some "#$ methods00
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 23/64
.ransitioning from :3.• 9or most components+ a 8 *efore
name – 'utton → J'utton+ "pplet → J"pplet + L
• 3or! from Components o$t to
Containers – :ing to top-leel containers ifferent M
elegate
05
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 24/64
9rames
• 9rame is a #ino# that is not containeinsie another #ino# 9rame is the *asis
to contain other $ser interface components
in 8aa GUI applications• .he 89rame class can *e $se to create
#ino#s
• 9or S#ing GUI programs+ $se 89rame class
to create #io#s
07
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 25/64
89rame
• JFrame is the application #ino# class
• It is special? it ra#s the #ino# an
interacts #ith the operating s%stem
• 3hen a JFrame is create+ an inner
container calle the contentPane is
a$tomaticall% create
• 3e on4t ra# graphics irectl% on
JFrame; #e ra# on the contentPane
0;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 26/64
:natom% of a 89rame
title bar
minimie
maximie
close
$he contentPane holds your
content; created automatically
when a Jrame is created
0<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 27/64
&xample' &mpt% 9rame
package swinglab;
import java.awt.*;import javax.swing.*;// extends keyword makes Calc a JFrame
public class Calc extends JFrame{ public Calc! { // get t"e content#ane and assign it to cp Container cp $ getContent#ane!; // exit program w"en user closes t"e window set%e&aultClose'peration()+,'-,C'(!; // sets t"e layout; will be covered in later slide
cp.setayoutnew Flowayout!!; // sets title to 01y Funky Calculator0
set+itle01y Funky Calculator0!; seti2e34445644!; // Frame "as 4 de&ault si2e
7 0@
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 28/64
&xample contin$e
. . .
public static void main tring89 args!{
Calc trial $ new Calc!;
// Frame is invisible by de&ault
trial.set:isibletrue!;
// main met"od exits but user inter&ace// stays alive
7
7
0A
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 29/64
Using the 8Panel Class
• : panel is a t%pe of container that4s esigne to hol
a gro$p of components so the% can *e ispla%e on
a frame .he normal #a% to ispla% a gro$p of
controls s$ch as text fiels+ la*els+ *$ttons+ an otherGUI #igets is to a those controls to a panel+ an
then a the panel to the frame
•2o$ can *%pass the panel an a the controlsirectl% to the frame if %o$ #ant+ *$t $sing a separate
panel to hol the frames control is almost al#a%s a
goo iea
0B
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 30/64
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 31/64
Usef$l 8Panel Constr$ctors
an ethos (Cont=)
'ethod "escriptionvoid add Component c! :s the specifie
component to thepanel
void remove Componentc!
void setayoutayout1anager layout!
emoes the specifiecomponent from the
panelSets the la%o$t manager $se tocontrol ho# components arearrange #hen the panel isispla%e .he efa$lt is the
9Io#a%o$t manager 51
Usef$l 8Panel Constr$ctors an
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 32/64
Usef$l 8Panel Constr$ctors an
ethos (Cont=)
'ethod "escriptionvoid setocation intx5 int y!
void seti2e int widt"5 int "eig"t!
void set+ool+ip+exttring text!
Sets the x an % positionof the frame-screen .hetop-left corner of the
screen is + Sets the siKe of the frame tothe specifie #ith anheightSets the tooltip text that4sispla%e if the $ser reststhe mo$se oer an empt%
part of the panel 50
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 33/64
&xample' package swinglab;
import java.awt.*;import javax.swing.*;
public class Calc extends JFrame{ private J#anel entry#anel;
private J#anel answer#anel; public Calc! {Container cp $ getContent#ane!;
set%e&aultClose'peration()+,'-,C'(!;cp.setackgroundColor.w"ite!;
set+itle01y Funky Calculator0!;seti2e34445644!;
entry#anel $ new J#anel!; entry#anel.setackgroundColor.orange!; answer#anel $ new J#anel!;
answer#anel.setackgroundColor.yellow!;
// . . . 55
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 34/64
&xample'
cp.addentry#anel!; cp.addanswer#anel!;
7 public static void main tring89 args!{ Calc trial $ new Calc!; trial.set:isibletrue!; 77
57
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 35/64
Colors
• .here are 15 preefine colors
• 2o$ can access them $sing Color.x #here
x is – orange+ pin!+ c%an+ magenta+ %ello#+ *lac!+ *l$e+
#hite+ gra%+ lightGra%+ ar!Gra%+ re+ green
• 2o$ can efine %o$r o#n colors
Color ugly $ new Color<45=453>4!;
//?@redAgreenAblue!; values between 4A>BB;
5;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 36/64
9rames+ Panes an Panels
5<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 37/64
JComponents• JComponent: .he *ase class for all S#ing components
except top-leel containers
• JComponents o not exten their :3. co$nterparts' – 9or example+ the JButton class is not a s$*class (irect or
inirect) of Button
• ote that JComponents are containers
• Jo#eer+ some S#ing components are notJComponents – 9or example+ some S#ing containers are irect s$*classes of
their :3. co$nterparts
• Its s$*classes present information or interact #ith the
$ser – &xamples'la*els(JLabels)+ *$ttons(JButtons)+textfiels(JTextField)
- Containers are some 8Components that are esigne to holother components (no interaction #ith the $ser)
- &xamples' JPanel, JScrollPane5@
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 38/64
Using a*els
• : la*el is a component that simpl% ispla%s
text a*els are $se for a ariet% of
p$rposes' to ispla% captions for other
controls s$ch as text fiels or com*o *oxes+
to ispla% informational messages+ or to
sho# the res$lts of a calc$lation or a
ata*ase loo!$p
5A
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 39/64
Using a*els
• : la*el can also ispla% an image+ or it can
ispla% *oth an image an some text :n
%o$ hae complete control oer the
appearance of the text
• 2o$ can specif% the font+ siKe+ #hether the
text is *ol+ italic+ or $nerline+ #hat color
the text is ispla%e as+ an so on
5B
Usef$l 8a*els Constr$ctors an
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 40/64
Usef$l 8a*els Constr$ctors an
ethosConstructor "escription
Jabel ! Creates a ne# la*el #ith noinitial text
'ethod "escription
tring get+ext ! et$rns the text ispla%e*% the la*el
void set+ext tringtext!void set+ool+ip+exttring text!
Sets the text ispla%e *%
the la*elSets the tooltip text that4sispla%e if the $ser reststhe mo$se oer the la*el
for a fe# moments 7
S#ing &xample
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 41/64
S#ing &xample
import java.awt.*;import javax.swing.*;
public class 1y+est extends JFrame { Jabel myabel $ new Jabel0ello5 DorldE0!; public 1y+est! {
super01y+est0!; seti2e<B45 344!; getContent#ane!.addmyabel!;
set%e&aultClose'peration()+,'-,C'(!;set:isibletrue!;
7
public static void main tring args89! { 1y+est m $ new 1y+est!; 7
7 71
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 42/64
Simple S#ing :pplication
70
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 43/64
Creating /$ttons
• ext to la*els+ the S#ing component $se
most is the 8/$tton component #hich
creates a *$tton the $ser can clic!
• .he constr$ctors of the 8/$tton class are
similar to the constr$ctors for the 8a*el
class 2o$ can either create an empt%
*$tton or a *$tton #ith text
75
U f l 8/ tt C t t
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 44/64
Usef$l 8/$tton Constr$ctors
an ethos
Constructor "escriptionJutton ! Creates a ne# *$tton
#ith no initial textJutton tring text!
tring get+ext !
Creates a ne# *$tton#ith the specifie textet$rns the textispla%e *% the*$tton
77
Usef l 8/ tton Constr ctors an
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 45/64
Usef$l 8/$tton Constr$ctors an
ethos (Cont=)'ethod "escriptionvoidset?ollover(nabledboolean value!
&na*les or isa*les therolloer effect+ #hich
ca$ses the *orer to getthic!er #hen the mo$semoes oer the *$tton.he efa$lt setting is tr$e
(rolloer effect ena*le)
void set(nabledboolean value!
&na*les or isa*les the*$tton .he efa$lt setting is
tr$e (ena*le)7;
Usef$l 8/$tton Constr$ctors
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 46/64
Usef$l 8/$tton Constr$ctors
an ethos (Cont=)
'ethod "escription
void set+exttring text!
Sets the text ispla%e *% the*$tton
void set+ool+ip+exttring text!
Sets the tooltip text that4sispla%e if the $ser lets themo$se rest oer the *$tton
void set:isibleboolean value! Sho#s or hies the *$tton
.he efa$lt setting is tr$e (the*$tton is isi*le)
7<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 47/64
8.ext9iel - 8Pass3or9iel
• .he% are single-line areas in #hich text can *eentere *% the $ser from the !e%*oar or text cansimpl% *e ispla%e
• *onstructors – 8.ext9iel(int col$mns)
• Creates an empt% text fiel #ith the specifie n$m*er ofcol$mns
– 8.ext9iel(String text)
– Creates a text fiel initialiKe #ith the specifie text
– 8.ext9iel(String text+ int col$mns) – Creates a text fiel initialiKe #ith the specifie text an the
col$mn siKe
– 8.ext9iel (String+ int maxim$m$m*erCharaters)7@
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 48/64
• +ethods – int getColomns(int col)? – oi setColo$mns(int col)? – get.ext()
• et$rns the string from the text fiel – set.ext(String text)
• P$ts the gien string in the text fiel
– set&ita*le(*oolean eita*le)• &na*les or isa*les the text fiel to *e eite /% efa$lt+
eita*le is tr$e – setCol$mns(int)
• Sets the n$m*er of col$mns in this text fiel .he length ofthe text fiel is changea*le
7A
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 49/64
• 3hen the $ser t%pes ata into them an presses
the Enter !e%+ an action eent occ$rs If the
program registers an eent listener+ the listener
processes the eent an can $se the ata in thetext fiel at the time of the eent in the program
– JTextField textField1 = new JTextField("This is the
initial text");
– JTextField textField2 = new JTextField("Initial text",columns);
– textField.setText(“hi);
7B
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 50/64
assword*ield class extens 8.ext9iel class• +ethods
– CharN O getPass#or()? MM.o o*tain the pass#orTe+tArea' a component $se to accept m$ltiple
line text
*onstructors
– 8.ext:rea( )• Defa$lt constr$ctor - Create an empt% text area
• 8.ext:rea(int ro#s+ int col$mns) – Creates a text area #ith the specifie n$m*er of
ro#s an col$mns
• 8.ext:rea(String s+ int ro#s+ int col$mns) – Creates a text area #ith the initial text an
the n$m*er of ro#s an col$mns specifie
;
,th C t
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 51/64
,ther Components
• Combo-o+ – rop-o#n list of items from #hich the $ser can
ma!es selection *% clic!ing an item or possi*l%*% t%ping into the *ox
– *onstructor • 8Com*o/ox(String strNO)?
– ethos• getSelecteItem()?MMret$rns the selecte item
• getSelecteInex()?• setaxim$mo#Co$nt(int n) - maxim$m of n items
#ill *e ispla%e if there are more items asscroll*ar
;1
import java.awt.*;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 52/64
import javax.swing.*;import java.util.*;
public class Calc extends JFrame{
private Jabel letabel;
private Jabel answerabel; private J+extField num3; private J+extField num>;
private JComboox operation; private Jutton calculate; private Jutton uit; private J#anel entry#anel; private J#anel answer#anel;
static &inal tring G%%,'# $ 0G%%+'-0; static &inal tring H,'# $ 0H+?GC+'-0; static &inal tring 1H,'# $ 01H+#CG+'-0; static &inal tring %:,'# $ 0%:'-0;
;0
i t t ti &i l i t )I( 3444
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 53/64
private static &inal int )I( $ 3444; private static &inal int I( $ 644;
public Calc! {
Container cp $ getContent#ane!; set%e&aultClose'peration()+,'-,C'(!; cp.setayoutnew Flowayout!!; cp.setackgroundColor.D+(!; set+itle01y Funky Calculator0!;
seti2e)I(5I(!;
entry#anel $ new J#anel!;entry#anel.setackgroundColor.'?G-@(!;
answer#anel $ new J#anel!; answer#anel.setackgroundColor.('D!;
letabel $ new Jabel0etKs CalculateE0!; entry#anel.addletabel!; letabel.setForegroundColor.@?((-!; ;5
num3 $ new J+extField03st -umber0 34!;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 54/64
num3 $ new J+extField03st -umber05 34!; entry#anel.addnum3!; num3.setackgroundColor. @+,@?G!;
num>$ new J+extField0>nd -umber05 34!; entry#anel.addnum>!; num>.setackgroundColor.@+,@?G!;
operation $ new JComboox!;
operation.addtemG%%,'#!; operation.addtemH,'#!; operation.addtem1H,'#!; operation.addtem%:,'#!;
entry#anel.addoperation!;
operation.setackgroundColor.H(!; answerabel $ new Jabel0Gnswer0!;
;7
entry#anel.addanswerabel!;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 55/64
answerabel.setForegroundColor.red!;calculate $ new Jutton0Calculate0!;
calculate.setackgroundColor.pink!; answer#anel.addcalculate!;
uit $ new Jutton0Luit0!; answer#anel.adduit!; Color uitter $ new ColorB453445B4!; uit.setackgrounduitter!;
cp.addentry#anel!;
cp.addanswer#anel!;
7
public static void maintring89 args!{ Calc trial $ new Calc!; trial.set:isibletrue!; 7
7;;
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 56/64
;<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 57/64
a%o$t anagers
• :ssociate #ith containers
• :$tomate the la%o$t of elements
– 3hen elements are ae to the container
– 3hen the #ino# is resiKe• a$tomaticall% a,$st the positions an siKes of the elements
• a%o$t managers control'
– 3here components appear
– 3hat siKes the% are
– Jo# the% react #hen the% are resiKe
;@
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 58/64
Jierarch% of a%o$t anagers
;A
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 59/64
/orera%o$t
• It $ses fie areas to hol components'
north+ south+ east+ west+ an center
• :ll extra space is place in the center
area
;B
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 60/64
9lo#a%o$t
• It simpl% la%s o$t components from left to
right+ starting ne# ro#s if necessar%
<
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 61/64
Cara%o$t
• Use the Cara%o$t class #hen %o$ hae
an area that can contain ifferent
components at ifferent times
<1
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 62/64
Gria%o$t
• Gria%o$ts simpl% ma!e a *$nch of
Components hae e6$al siKe+ ispla%ing
them in the re6$este n$m*er of ro#s an
col$mns
<0
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 63/64
Using a%o$t anagers
/orera%o$t
Mnameo&containerN.setayoutnew orderayout!!;
3hen aing components to the container'
Mnameo&containerN.addMnameo&componentN5orderayout.?(@'-!;
#here &GI> is either >.J+ S>U.J+ 3&S.+
C&.& > &:S.J#anel panel $ new J#anel!; // de&ault
// Flowayout
panel.setayoutnew orderayout!!;
panel.addbutton5orderayout.-'?+!;<5
7/23/2019 Advanced Programming - Chapter 6.ppt
http://slidepdf.com/reader/full/advanced-programming-chapter-6ppt 64/64
Using a%o$t anagers
9lo#a%o$t<nameocontainer!.setLa"out#ne$ Flo$La"out#%%;
3hen aing components to the container'<nameocontainer!.add#<nameocomponent!%;
JPanel panel & ne$ JPanel#%; '' deault
'' Flo$La"out
''ollo$in( line is redundant
panel.setLa"out#ne$ Flo$La"out#%%;