6.lập trình gui trong java
TRANSCRIPT
-
7/25/2019 6.Lp trnh GUI trong Java
1/132
Lp Trnh Giao Din
-
7/25/2019 6.Lp trnh GUI trong Java
2/132
GUIs
Gi AWTc s dng trong cc phin bn trc !d"#$%&!a'a%(
Swingc a ')o trong phin bn !d"#$% ') c nhi*+ch,c n-ng h.n /0T
Swing Componentsth1ng c tn b2t 3+ 'i 4546 /0T c 7pPanel6 89ing c 7p t.ng ,ng 7)JPanel
-
7/25/2019 6.Lp trnh GUI trong Java
3/132
Graphical User Interface (GUI)
T (Abstract Windows Toolkits) importjava.awt.*
in" (#ava $o%ndation &lasses 'acka"e) importjava.swin".*
import java.awt.*
import java.awt.event.*
Tt c!win" component c+ t,n b-t /% v0i #1
-
7/25/2019 6.Lp trnh GUI trong Java
4/132
The Swing API
Gi 89ing 7) # th)nh ph3n c:aJava Foundation Classes&5;?t tp cc ch,c n-ng gi@p ABC dng cc,ng dng GEF$
-
7/25/2019 6.Lp trnh GUI trong Java
5/132
GUI
Java GUI hierarchy
Hb!Ict
Insion
;ont
FontMetrics
Graphics
-
7/25/2019 6.Lp trnh GUI trong Java
6/132
GUI
Java GUI hierarchy
JComponent
JMenuBar
Astract!utton
JScrollPane
JPanel
JTe"tComponent
JLabel
JomboBo!
JMenuItem
JButton
J"e!tArea
J"e!tFiel# JPa$$wor#Fiel#
% etc& Swing
%
lightweight heavyweight
-
7/25/2019 6.Lp trnh GUI trong Java
7/132
Swing Components
89ing ponInts c nhi*+ >,c "hc nha+
,c n)C +i Mnh cc ponInt trong # ,ng dng
GEF "Nt hp 'i nha+$
-
7/25/2019 6.Lp trnh GUI trong Java
8/132
2h3i ni4m container
L thnh phn m c th cha cc thnh phn khc,c th
v v t mu. Frame/JFrame, Panel/JPanel, Dial!/JDial!,
"crllPane/J"crllPane, # $%n cmpnent v khun! cha
cntainer&'(.a))*cmp&'(+
L- thn! tin ca cmpnent '(ect0ame.!et#* +
$n thn! tin ch cmpnent '(ect0ame.1et#* +
-
7/25/2019 6.Lp trnh GUI trong Java
9/132
5"%6,n t-c 76 d8n" GUI tron"java
L2a ch3n 4 cntainer5 Frame/JFrame, 6in)7/J6in)7,
Dial!/JDial!, # 89 cc :i;u khin5 *'uttn1, tea cc :i;u khin v v?n! cha "%p
-
7/25/2019 6.Lp trnh GUI trong Java
10/132
&ontainment 9ierarch6
"op'level container( JFrame) JDialog) JApplet
-
7/25/2019 6.Lp trnh GUI trong Java
11/132
:; ? ar"s) @ $rame frame B new $rame()
frame.set:isible(tr%e) 99
AppFrame-&*ava
clo$ing win#ow
456
756
-
7/25/2019 6.Lp trnh GUI trong Java
12/132
Containment Hierarchy
Top LI'I7IdiatIic
ponInt P
/to>ic
ponInt P
/to>ic
ponInt P
-
7/25/2019 6.Lp trnh GUI trong Java
13/132
Containment Hierarchy
"op Level
&5;ra>I(
Interme#iate
&5JanI7(
Atomic
&5Q+tton(
Atomic
&5LabI7(
-
7/25/2019 6.Lp trnh GUI trong Java
14/132
Containment Hierarchy
Top LI'I7
-
7/25/2019 6.Lp trnh GUI trong Java
15/132
Containment Hierarchy
FntIr>IdiatI
-
7/25/2019 6.Lp trnh GUI trong Java
16/132
Containment Hierarchy
/to>ic ponInts6 YhZng ch,a cc ponInts "hc6 DVng R c+ng c=p thZng tin Nn ng1i s dng6 WoXc 7=C thZng tin t[ ng1i s dng
6 S dU 5Q+tton 5LabI7 5boQoA 5TIAt;iI7d
5Tab7I
-
7/25/2019 6.Lp trnh GUI trong Java
17/132
Containment Hierarchy
\Oi Top LI'I7 IdiatI ponInt phig2n ')o contInt panI6S d 65;ra>I ]ra>I ^ nI9 5;ra>I&$$$(_
5JanI7panI ^ nI9 5JanI7&(_]ra>I$gIt
-
7/25/2019 6.Lp trnh GUI trong Java
18/132
2h3i ni4m Ca6o%t Dana"er
Ec l9i laut khc nhau5
Fl7 Laut r)er Laut $ri) Laut
$ri)a! Laut 0ull Laut
# 8rGnh HuIn lC laut :>c thi@t lKp 'n! cch !3i ph>Mn!
thc NsetCa6o%t( )O
-
7/25/2019 6.Lp trnh GUI trong Java
19/132
$lowCa6o%t
L trGnh HuIn lC laut mc :Qnh ch cc applet v cc panel
RSi Fl7Laut cc thnh phn 1 :>c
-
7/25/2019 6.Lp trnh GUI trong Java
20/132
$lowCa6o%t
;7o9 LaCo+t 6 LI]t and `ight /7ignId
-
7/25/2019 6.Lp trnh GUI trong Java
21/132
EorderCa6o%t
L trGnh HuIn lC laut mc :Qnh ch 6in)7, Frame vDial!
8rGnh HuIn lC n c th Sn! 0&W8Y, [\"8,"&]8Y, 6["8 v E[08[W ca cntainer
R^ )_5 = thAm m`t thnh phn v v?n! 0rth cacntainer uttn '4V ne7 uttn*0rth uttnb+ 1etLaut*ne7 r)erLaut* ++ a))*'4, r)erLaut.0&W8Y+
-
7/25/2019 6.Lp trnh GUI trong Java
22/132
EorderCa6o%t
-
7/25/2019 6.Lp trnh GUI trong Java
23/132
GridCa6o%t
W tr 'ic chia containIr th)nh>?t 7i
-
7/25/2019 6.Lp trnh GUI trong Java
24/132
GridEa"Ca6o%t
tr^ cc thnh phn m`t cch ch^nh Sc Ec thnh phn :>c Sn! tT trif
1an!fphIi v trAnfSi
Ym cn1tructr $ri)a!Laut !' V ne7 $ri)a!Laut* +
-
7/25/2019 6.Lp trnh GUI trong Java
25/132
GridEa"Ca6o%t
Lp eGri#BagLayouton$traint$f 7+tr t=t c cc thZng tin >) 7p GridLaCo+tC+ c3+U SM tr ') "ch th+c >i th)nhph3n
-
7/25/2019 6.Lp trnh GUI trong Java
26/132
5%llCa6o%t
T do trong 'ic Mnh 'M tr ') "ch thc c:a cc co>ponInts
Frame $r % new Frame&'(ullLayout )emo'*+
$r,setLayout&null*+
-
7/25/2019 6.Lp trnh GUI trong Java
27/132
Swing Components
!F
-
7/25/2019 6.Lp trnh GUI trong Java
28/132
!F
-
7/25/2019 6.Lp trnh GUI trong Java
29/132
add set "et method
'hHn" thJc dKn" L "-ncomponentsM
object5ame.add(1 )
'hHn" thJc dKn" L l6 th%Nc t;nhM
object5ame."et( )
'hHn" thJc dKn" L "3n th%Nct;nhM
object5ame.set( )
O. Usin" !win" &omponents
DP hQ h F lR ki4
-
7/25/2019 6.Lp trnh GUI trong Java
30/132
DP hQnh F lR s8 ki4n
Z hnh A 7 s "inU
Pg+jn pht sinh s "in &I'Int so+rcI(
8 "in &I'Int ob!Ict(
Q? 72ng nghI s "in &I'Int 7istInIr(
SF CR !8 2i4n
-
7/25/2019 6.Lp trnh GUI trong Java
31/132
:hai b;o l
$? @in
public cla$$ Myla$$ implement$ CventLi$tener
Ei t c;c phHng thc trong li$tener inter0ace&
K #( ActionLi$tener public voi# actionPer0orme#1ActionCvent e2 .
&&&//co#e that react$ to the action&&& 9
GNn bO != l> vEo component $omeomponent&a##CventLi$tener1
in$tance0Myla$$2,
SF CR !8 2i4n
9 h N ki4 l- h
-
7/25/2019 6.Lp trnh GUI trong Java
32/132
9nh Nn" s8 ki4n l-n" n"he
=i t>n! "2 kin ` l%n! n!he6in)7, Frame, # 6in)7[vent 6in)7Li1tener
uttn, genuXtem, # \ctin[vent \ctinLi1tener
8e
-
7/25/2019 6.Lp trnh GUI trong Java
33/132
JLabelLaeldVng R hiRn thM >?t ch+i '-n bn thZng th1ng
nhk> >Z t th> thZng tin cho cc i tng "hc$
-
7/25/2019 6.Lp trnh GUI trong Java
34/132
Cc thuc tnh JLabel
text
icon
horizontalAlignment
verticalAlignment
Esing LabI7s
-
7/25/2019 6.Lp trnh GUI trong Java
35/132
V D
JButton
-
7/25/2019 6.Lp trnh GUI trong Java
36/132
JButton!utton7) >?t th)nh ph3n gBC ra >?t s "in h)nh ?ng
"hi c "ch ch+?t$
-
7/25/2019 6.Lp trnh GUI trong Java
37/132
Cc thuc tnhJButton text
icon
mnemonic
horizontalAlignment
verticalAlignment horizontalTextPosition
verticalTextPosition
Esing Q+ttons
pngccskinJButton
-
7/25/2019 6.Lp trnh GUI trong Java
38/132
p ng cc s kinJButton ublic voi! actionPer"orme!(Action#vent e)
$
%% &et the button label
String action'omman! egetAction'omman!()*
%% +ae sure the event source is Le"t button
i" (egetSource() instanceo" JButton) %% +ae sure it is the right button
i" (-Le"t-e.uals(action'omman!))
S/stemoutrintln (-Button
resse!0-)* 1
-
7/25/2019 6.Lp trnh GUI trong Java
39/132
Menus
-
7/25/2019 6.Lp trnh GUI trong Java
40/132
Menus
5a'a c+ng c=p >?t s 7p l J+enuBar J+enu
J+enuItem, J'hecBox+enuItem, ')J2a!ioButton+enuItem 3R thc thi >In+ trong >?t
]ra>I$
\?t J4ramehoXc JAletc thR ch,a >?t menu ar
trn c g2n ccpull-down menu$ In+ ch,a cc
menu itemR ng1i dVng 7a chOn &hoXc btmt2t($ \In+ bar
c thR c AI> nh >?t c=+ tr@c R h tr cc >In+$
Menu Demo
-
7/25/2019 6.Lp trnh GUI trong Java
41/132
Menu Demo
LpJMenuBar
-
7/25/2019 6.Lp trnh GUI trong Java
42/132
LpJMenuBarMenu arch,a cc >In+_ >In+ bar ch c thR c th>
')o # ]ra>I$ on codI sa+ to ') th> >?t 5\In+Qar')o
# ]ra>IU
J4rame " ne5 J4rame()*
"setSize(677, 877)*
"set9isible(true)*
J+enuBar mb ne5 J+enuBar()*
"setJ+enuBar(mb)*
LpMenu
-
7/25/2019 6.Lp trnh GUI trong Java
43/132
LpMenuQn g2n cc >In+ ')o >?t J+enuBar$ on codI sa+ to
% >In+ 4ile') :el ') th> ch@ng ')o J+enuBar
mbU
J+enu "ile+enu ne5 J+enu(-4ile-, "alse)*
J+enu hel+enu ne5 J+enu(-:el-, true)*
mba!!("ile+enu)*
mba!!(hel+enu)*
LpJMenuItem
-
7/25/2019 6.Lp trnh GUI trong Java
44/132
LpJMenuItemon codI sa+ th> cc >c chOn &>In+ itI>( ') cc
sIparator trong >In+ "ile+enuU
"ile+enua!!(ne5 J+enuItem(-;e5-))*
"ile+enua!!(ne5 J+enuItem(-
-
7/25/2019 6.Lp trnh GUI trong Java
45/132
Submenus
Qn c thR th> cc s+b>In+s ')o cc >In+ itI>$ on codI sa+ th> cc
s+b>In+ qEniA qPT ') q0in ')o trong >c chOn q8o]t9arI$
J+enu so"t5are:elSub+enu ne5 J+enu(-So"t5are-)*
J+enu har!5are:elSub+enu ne5 J+enu(-:ar!5are-)*
hel+enua!!(so"t5are:elSub+enu)*
hel+enua!!(har!5are:elSub+enu)*
so"t5are:elSub+enua!!(ne5 J+enuItem(-=nix-))*
so"t5are:elSub+enua!!(ne5 J+enuItem(-;T-))*
so"t5are:elSub+enua!!(ne5 J+enuItem(->in?@-))*
Submenu Demo
-
7/25/2019 6.Lp trnh GUI trong Java
46/132
Submenu Demo
-
7/25/2019 6.Lp trnh GUI trong Java
47/132
SWING Dialog Boxes
-
7/25/2019 6.Lp trnh GUI trong Java
48/132
Phn thZng tin t[ ng1i s dng
s 7i+
danh >c tp tin
WiRn thM "Nt +
hiRn thM thZng tin cnh bo
in "Nt + 7n >)n hnh
JOptionPane
-
7/25/2019 6.Lp trnh GUI trong Java
49/132
J
-
7/25/2019 6.Lp trnh GUI trong Java
50/132
JIthod ca77
JOptionPane
-
7/25/2019 6.Lp trnh GUI trong Java
51/132
sho5'on"irmialog Ass a con"irming.uestion, lie /es%no%cancel
sho5Inutialog Promt "or some inut
sho5+essageialog Tell the user aboutsomething that has
haene!
sho5
-
7/25/2019 6.Lp trnh GUI trong Java
52/132
%% static metho! call
J
-
7/25/2019 6.Lp trnh GUI trong Java
53/132
Using JOptionPane
-
7/25/2019 6.Lp trnh GUI trong Java
54/132
%% static metho! call
J
-
7/25/2019 6.Lp trnh GUI trong Java
55/132
J
-
7/25/2019 6.Lp trnh GUI trong Java
56/132
int sho9HpInDia7og&ponInt parInt(U >u h?p
thoi Oc tp tin int sho98a'IDia7og&ponInt parInt(U >u h?p
thoi 7+ tp tin 5;i7Iu h?p thoi
ghimOc tp tin ;i7I gIt8I7IctId;i7I&(U 7=C thZng tin '* tp tinmth >c
c chOn6 8tring gItJath&(
6 8tring gItPa>I&(
JFileChooser
-
7/25/2019 6.Lp trnh GUI trong Java
57/132
JFileChooser
-
7/25/2019 6.Lp trnh GUI trong Java
58/132
J4ile'hooserAPP2
-
7/25/2019 6.Lp trnh GUI trong Java
59/132
J4ile'hooser'A;'#LD
-
7/25/2019 6.Lp trnh GUI trong Java
60/132
"ilename "cget;ame()*
i" (return9alue J4ile'hooserAPP2
-
7/25/2019 6.Lp trnh GUI trong Java
61/132
-
7/25/2019 6.Lp trnh GUI trong Java
62/132
-
7/25/2019 6.Lp trnh GUI trong Java
63/132
Using JFileChooser
-
7/25/2019 6.Lp trnh GUI trong Java
64/132
Using JFileChooser
-
7/25/2019 6.Lp trnh GUI trong Java
65/132
Using JFileChooser
-
7/25/2019 6.Lp trnh GUI trong Java
66/132
a 4ileob!Ict
a String
Lc Tp Tin Hin Th
-
7/25/2019 6.Lp trnh GUI trong Java
67/132
;i7IPa>IvAtInsion;i7tIr&8tring dispc
8tring ]i7tIr(6;i7IPa>IvAtInsion;i7tIr ]i7tIr ^ nI9
;i7IPa>IvAtInsion;i7tIr& w5JG x GF;
F>agIsw w!pgw wgi]w(_ 5;i7IIvAtInsion;i7tIr ]i7tIr(
JTextField
-
7/25/2019 6.Lp trnh GUI trong Java
68/132
Text field 7) Z nhp d 7i+ dng '-n bn trn #dng$?t tIAt ]iI7d trng c s c?t Ac Mnh$
JText4iel!(String text)
To >?t tIAt ]iI7d 'i '-n bn c syn$
JText4iel!(String text, int columns)
To >?t tIAt ]iI7d 'i '-n bn c syn ') s c?t Ac Mnh$
-
7/25/2019 6.Lp trnh GUI trong Java
69/132
text
horizontalAlignment
e!itable
columns
Cc phng thcJTextField tT t()
-
7/25/2019 6.Lp trnh GUI trong Java
70/132
getText()
Tr '* ch+i " t trong tIAt ]iI7d$
setText(String text)
Xt ch+i " t trong tIAt ]iI7d$
set#!itable(boolean e!itable)
-
7/25/2019 6.Lp trnh GUI trong Java
71/132
TextArea7) "h+ng cho phzp ng1i s dng nhp ')o
nhi*+ dng '-n bn$
-
7/25/2019 6.Lp trnh GUI trong Java
72/132
text
e!itable columns
line>ra
5raSt/le>or! ro5s
line'ount
tabSize
Swing Button Classes
-
7/25/2019 6.Lp trnh GUI trong Java
73/132
|
JComponent
AbstractButton
JButton JToggleButton
JCheckBox JRadioButton
JCheckBoxandJRadioButton
-
7/25/2019 6.Lp trnh GUI trong Java
74/132
-
7/25/2019 6.Lp trnh GUI trong Java
75/132
c to ra "hi ng1i dVng chOn cc >c "hc nha+
trn 5
-
7/25/2019 6.Lp trnh GUI trong Java
76/132
'oid itI>8tatIv'Int I(U c gOi thi
h)nh "hi ng1i dVng chOn hoXc b} chOn # >c$
V D JCheckBox
-
7/25/2019 6.Lp trnh GUI trong Java
77/132
1 // Fi! "#!""$ CheckBoxTe%t!&a'a2 // Creatin Checkbox button%!34 // Ja'a core (ackae%5 im(ort&a'a!a)t!*+6 im(ort&a'a!a)t!e'ent!*+
CheckBoxTe%t!&a'a
-
7/25/2019 6.Lp trnh GUI trong Java
78/132
78 // Ja'a exten%ion (ackae%9 im(ort&a'ax!%)in!*+1011(ublic cla%%CheckBoxTe%t extend%JFrame ,12 (ri'ateJTextField -ield+13 (ri'ateJCheckBox bold. italic+1415 // %et u( 0I16 (ublicCheckBoxTe%t1217 ,18 %u(er1 3JCheckBox Te%t32+19
20 // et content (ane and %et it% la4out21 Container container 5 etContent6ane12+22 container!%etLa4out1 ne)Flo)La4out12 2+2324 // %et u( JTextField and %et it% -ont25 -ield 526 ne)JTextField1 37atch the -ont %t4le chane3. #82+27 -ield!%etFont1 ne)Font1 39eri-3. Font!6LAI:. ";2 2+28 container!add1 -ield 2+29
30 // create checkbox ob&ect%31 bold 5 ne)JCheckBox1 3Bold32+32 container!add1 bold 2+3334 italic 5 ne)JCheckBox1 3Italic32+35 container!add1 italic 2+
Line 13
Line 27
Lines 31-35DIc7arI t9o JCheckBoxinstancIs
8It JTextField]ont to8Iri] #~lpoint p7ain
FnstantiatI JCheckBoxs ]or bo7ding andita7iciing JTextFieldtIAt rIspIcti'I7C
-
7/25/2019 6.Lp trnh GUI trong Java
79/132
;# %et9ie1 #>. "882+;< %etDi%ible1 true2+;; E;;= // execute a((lication;> (ublic %tatic 'oidmain1 9trin ar%G 2;? ,; CheckBoxTe%t a((lication 5 ne)CheckBoxTe%t12+8" a((lication!%etHe-aultClo%e(eration1# JFrame!KIT:CL92+< E;
// (ri'ate inner cla%% -or ItemLi%tener e'ent handlin= (ri'ate cla%%CheckBox@andler im(lement%ItemLi%tener ,> (ri'ate int'alBold 5 Font!6LAI:+? (ri'ate int 'alItalic 5 Font!6LAI:+=8 // re%(ond to checkbox e'ent%=" (ublic 'oiditem9tateChaned1 Item'ent e'ent 2=# ,=< // (roce%% bold checkbox e'ent%=; i-1 e'ent!etItem12 55 bold 2
=== i-1 e'ent!et9tateChane12 55 Item'ent!9LCTH2=> 'alBold 5 Font!BLH+=? el%e= 'alBold 5 Font!6LAI:+>8
Lines 38-40
Line 61
0hIn +sIr sI7Icts JCheckBoxCheckBox@andler in'o"Is
>Ithod item9tateChane% o] a77rIgistIrId 7istInIrs
71 // (roce%% italic checkbox e'ent%72 i-1 e'ent!et9ource12 55 italic 27374 i-1 e'ent!et9tateChane12 55 Item'ent!9LCTH275 'alItalic 5 Font!ITALIC+76 el%e
CheckBoxTe%t!&a'a
-
7/25/2019 6.Lp trnh GUI trong Java
80/132
77 'alItalic 5 Font!6LAI:+7879 // %et text -ield -ont
80 -ield!%etFont181 ne)Font1 39eri-3. 'alBold 'alItalic. ";2 2+82 E8384 E // end (ri'ate inner cla%% CheckBox@andler8586 E // end cla%% CheckBoxTe%t
Lines 80-81
-
7/25/2019 6.Lp trnh GUI trong Java
81/132
1 // Fi! "#!"#$ RadioButtonTe%t!&a'a2 // Creatin radio button% u%in Buttonrou( and JRadioButton!34 // Ja'a core (ackae%5 im(ort&a'a!a)t!*+6 im(ort&a'a!a)t!e'ent!*+
RadioButtonTe%t!&a'a
Li 1415
-
7/25/2019 6.Lp trnh GUI trong Java
82/132
78 // Ja'a exten%ion (ackae%9 im(ort&a'ax!%)in!*+
1011(ublic cla%%RadioButtonTe%t extend%JFrame ,12 (ri'ateJTextField -ield+13 (ri'ateFont (lainFont. boldFont. italicFont. boldItalicFont+14 (ri'ateJRadioButton (lainButton. boldButton. italicButton.15 boldItalicButton+16 (ri'ateButtonrou( radiorou(+1718 // create 0I and -ont%19 (ublicRadioButtonTe%t12
20 ,21 %u(er1 3RadioButton Te%t32+2223 // et content (ane and %et it% la4out24 Container container 5 etContent6ane12+25 container!%etLa4out1 ne)Flo)La4out12 2+2627 // %et u( JTextField28 -ield 5
29 ne)JTextField1 37atch the -ont %t4le chane3. #2+30 container!add1 -ield 2+3132 // create radio button%33 (lainButton 5 ne)JRadioButton1 36lain3. true2+34 container!add1 (lainButton 2+35
Lines 14-15
Line 16
JRadioButtons nor>a77CappIar as a Buttonrou(
DIc7arI ]o+r JRadioButtoninstancIs
36 boldButton 5 ne)JRadioButton1 3Bold3. -al%e2+37 container!add1 boldButton 2+3839 italicButton 5 ne)JRadioButton1 3Italic3. -al%e2+40 container!add1 italicButton 2+4142 boldItalicButton 5 ne) JRadioButton1
RadioButtonTe%t!&a'a
Lines3644
FnstantiatI JRadioButtons ]or
-
7/25/2019 6.Lp trnh GUI trong Java
83/132
42 boldItalicButton 5 ne)JRadioButton143 3Bold/Italic3. -al%e2+44 container!add1 boldItalicButton 2+45
46 // rei%ter e'ent% -or JRadioButton%47 RadioButton@andler handler 5 ne)RadioButton@andler12+48 (lainButton!addItemLi%tener1 handler 2+49 boldButton!addItemLi%tener1 handler 2+50 italicButton!addItemLi%tener1 handler 2+51 boldItalicButton!addItemLi%tener1 handler 2+5253 // create loical relation%hi( bet)een JRadioButton%54 radiorou( 5 ne)Buttonrou(12+
55 radiorou(!add1 (lainButton 2+56 radiorou(!add1 boldButton 2+57 radiorou(!add1 italicButton 2+58 radiorou(!add1 boldItalicButton 2+5960 // create -ont ob&ect%61 (lainFont 5 ne)Font1 39eri-3. Font!6LAI:. ";2+62 boldFont 5 ne)Font1 39eri-3. Font!BLH. ";2+63 italicFont 5 ne)Font139eri-3. Font!ITALIC. ";2+64 boldItalicFont 5
65 ne)Font1 39eri-3. Font!BLH Font!ITALIC. ";2+66 -ield!%etFont1 (lainFont 2+6768 %et9ie1
-
7/25/2019 6.Lp trnh GUI trong Java
84/132
78 JFrame!KIT:CL92+79 E8081 // (ri'ate inner cla%% to handle radio button e'ent%82 (ri'ate cla%%RadioButton@andler im(lement%ItemLi%tener ,8384 // handle radio button e'ent%85 (ublic 'oiditem9tateChaned1 Item'ent e'ent 286 ,87 // u%er clicked (lainButton88 i-1 e'ent!etItem12 55 (lainButton 289 -ield!%etFont1 (lainFont 2+90
91 // u%er clicked boldButton92 el%e i-1e'ent!etItem12 55 boldButton 293 -ield!%etFont1 boldFont 2+9495 // u%er clicked italicButton96 el%e i-1e'ent!etItem12 55 italicButton 297 -ield!%etFont1 italicFont 2+9899 // u%er clicked boldItalicButton100 el%e i- 1e'ent!etItem12 55 boldItalicButton 2
101 -ield!%etFont1 boldItalicFont 2+102 E103104 E // end (ri'ate inner cla%% RadioButton@andler105106E // end cla%% RadioButtonTe%t
Lines 85-104
Lines 88-102
0hIn +sIr sI7Icts JRadioButtonRadioButton@andlerin'o"Is
>Ithod item9tateChanedo] a77rIgistIrId 7istInIrs
8It ]ont corrIsponding toJRadioButtonsI7IctId
JComboBox
JC b B
-
7/25/2019 6.Lp trnh GUI trong Java
85/132
JComboBox6 DVng R 7it " danh sch cc >c >) ng1i dVng c thR chOn
6
-
7/25/2019 6.Lp trnh GUI trong Java
86/132
1 // Fi! "#!"
-
7/25/2019 6.Lp trnh GUI trong Java
87/132
78 // Ja'a exten%ion (ackae%9 im(ort&a'ax!%)in!*+1011(ublic cla%%ComboBoxTe%t extend%JFrame ,12 (ri'ateJComboBox imae%ComboBox+13 (ri'ateJLabel label+1415 (ri'ate9trin name%G 516 , 3bu"!i-3. 3bu#!i-3. 3tra'elbu!i-3. 3buanim!i-3E+17 (ri'ateIcon icon%G 5 , ne)ImaeIcon1 name% 8G 2.18 ne)ImaeIcon1 name% "G 2. ne)ImaeIcon1 name% #G 2.19 ne)ImaeIcon1 name%
-
7/25/2019 6.Lp trnh GUI trong Java
88/132
1 12 244 label!%etIcon1 icon%45 imae%ComboBox!et9electedIndex12 G 2+46 E
4748 E // end anon4mou% inner cla%%4950 2+ // end call to addItemLi%tener5152 container!add1 imae%ComboBox 2+5354 // %et u( JLabel to di%(la4 ImaeIcon%55 label 5 ne)JLabel1 icon% 8G 2+
56 container!add1 label 2+5758 %et9ie1 Ithod
item9tateChanedo] a77 rIgistIrId 7istInIrs
8It appropriatI IcondIpInding on +sIr sI7Iction
JLi%t
57ist
-
7/25/2019 6.Lp trnh GUI trong Java
89/132
57ist6 Danh sch cc >c chOn
6 c
6 Jht sinh List8I7Ictionv'Int "hi ng1i dVng chOn
-
7/25/2019 6.Lp trnh GUI trong Java
90/132
1// Fi! "#!";$ Li%tTe%t!&a'a
2 // 9electin color% -rom a JLi%t!34 // Ja'a core (ackae%5 im(ort&a'a!a)t!*+67 // Ja'a exten%ion (ackae%8 i t & i *
Li%tTe%t!&a'a
Line34
-
7/25/2019 6.Lp trnh GUI trong Java
91/132
8 im(ort&a'ax!%)in!*+9 im(ort &a'ax!%)in!e'ent!*+10
11(ublic cla%% Li%tTe%t extend%JFrame ,12 (ri'ateJLi%t colorLi%t+13 (ri'ateContainer container+1415 (ri'ate9trin color:ame%G 5 , 3Black3. 3Blue3. 3C4an3.16 3Hark ra43. 3ra43. 3reen3.3Liht ra43. 3Maenta3.17 3rane3. 36ink3. 3Red3. 37hite3. 3Nello)3E+1819 (ri'ateColor color%G 5 , Color!black. Color!blue.20 Color!c4an. Color!darkra4. Color!ra4. Color!reen.21 Color!lihtra4. Color!maenta. Color!orane. Color!(ink.22 Color!red. Color!)hite. Color!4ello)E+2324 // %et u( 0I25 (ublicLi%tTe%t1226 ,27 %u(er1 3Li%t Te%t32+2829 // et content (ane and %et it% la4out
30 container 5 etContent6ane12+31 container!%etLa4out1 ne)Flo)La4out12 2+3233 // create a li%t )ith item% in color:ame% arra434 colorLi%t 5 ne)JLi%t1 color:ame% 2+35 colorLi%t!%etDi%ibleRo)Count1 2+
Line 34
EsI color:ame%arraC topop+7atI JLi%t
3637 // do not allo) multi(le %election%38 colorLi%t!%et9electionMode139 Li%t9electionModel!9I:L9LCTI: 2+4041 // add a J9croll6ane containin JLi%t to content (ane42 container!add1 ne)J9croll6ane1 colorLi%t 2 2+
Li%tTe%t!&a'a
Lines38-39
JLi%ta77o9s sing7I sI7Ictions
-
7/25/2019 6.Lp trnh GUI trong Java
92/132
4344 // %et u( e'ent handler45 colorLi%t!addLi%t9electionLi%tener14647 // anon4mou% inner cla%% -or li%t %election e'ent%48 ne)Li%t9electionLi%tener12 ,4950 // handle li%t %election e'ent%51 (ublic 'oid'alueChaned1 Li%t9election'ent e'ent 252 ,53 container!%etBackround154 color% colorLi%t!et9electedIndex12 G 2+
55 E5657 E // end anon4mou% inner cla%%5859 2+ // end call to addLi%t9electionLi%tener6061 %et9ie1 Ithod 'alueChanedo] a77rIgistIrId 7istInIrs
8It appropriatI bac"gro+nddIpInding on +sIr sI7Iction
70 a((lication!%etHe-aultClo%e(eration171 JFrame!KIT:CL92+72 E7374 E // end cla%% Li%tTe%t Li%tTe%t!&a'a
-
7/25/2019 6.Lp trnh GUI trong Java
93/132
Multiple-Selection Lists
\+7tip7IlsI7Iction 7ist
-
7/25/2019 6.Lp trnh GUI trong Java
94/132
\+7tip7I sI7Iction 7ist6 c trn Jli%t
1 // Fi! "#!"$ Multi(le9election!&a'a2 // Co(4in item% -rom one Li%t to another!34 // Ja'a core (ackae%5 im(ort&a'a!a)t!*+6 im(ort&a'a!a)t!e'ent!*+78 // Ja'a exten%ion (ackae%
Multi(le9election!&'a
Li 29
-
7/25/2019 6.Lp trnh GUI trong Java
95/132
8 // Ja'a exten%ion (ackae%9 im(ort&a'ax!%)in!*+10
11(ublic cla%%Multi(le9election extend%JFrame ,12 (ri'ateJLi%t colorLi%t. co(4Li%t+13 (ri'ateJButton co(4Button+1415 (ri'ate9trin color:ame%G 5 , 3Black3. 3Blue3. 3C4an3.16 3Hark ra43. 3ra43. 3reen3. 3Liht ra43.17 3Maenta3. 3rane3. 36ink3. 3Red3. 37hite3. 3Nello)3E+1819 // %et u( 0I20 (ublicMulti(le9election1221 ,22 %u(er1 3Multi(le 9election Li%t%32+2324 // et content (ane and %et it% la4out25 Container container 5 etContent6ane12+26 container!%etLa4out1 ne)Flo)La4out12 2+2728 // %et u( JLi%t colorLi%t29 colorLi%t 5 ne)JLi%t1 color:ame% 2+
30 colorLi%t!%etDi%ibleRo)Count1 2+31 colorLi%t!%etFixedCell@eiht1 "2+32 colorLi%t!%et9electionMode133 Li%t9electionModel!M0LTI6LI:TRDAL9LCTI:2+34 container!add1 ne)J9croll6ane1 colorLi%t 2 2+35
Line 29
Lines 32-33
EsI color:ame%arraC topop+7atI JLi%t
JLi%tcolorLi%ta77o9s>+7tip7I sI7Ictions
36 // create co(4 button and rei%ter it% li%tener37 co(4Button 5 ne)JButton1 3Co(4 OOO3 2+3839 co(4Button!addActionLi%tener14041 // anon4mou% inner cla%% -or button e'ent42 ne)ActionLi%tener12 ,43
Multi(le9election!&'a
Li 4849
-
7/25/2019 6.Lp trnh GUI trong Java
96/132
344 // handle button e'ent45 (ublic 'oidaction6er-ormed1 Action'ent e'ent 2
46 ,47 // (lace %elected 'alue% in co(4Li%t48 co(4Li%t!%etLi%tHata149 colorLi%t!et9electedDalue%12 2+50 E5152 E // end anon4mou% inner cla%%5354 2+ // end call to addActionLi%tener5556 container!add1 co(4Button 2+5758 // %et u( JLi%t co(4Li%t59 co(4Li%t 5 ne)JLi%t12+60 co(4Li%t!%etDi%ibleRo)Count1 2+61 co(4Li%t!%etFixedCell7idth1 "882+62 co(4Li%t!%etFixedCell@eiht1 "2+63 co(4Li%t!%et9electionMode164 Li%t9electionModel!9I:LI:TRDAL9LCTI:2+
65 container!add1 ne)J9croll6ane1 co(4Li%t 2 2+6667 %et9ie1
-
7/25/2019 6.Lp trnh GUI trong Java
97/132
78 E7980 E // end cla%% Multi(le9election
#Tabbed'ane
-
7/25/2019 6.Lp trnh GUI trong Java
98/132
#Table'ane
-
7/25/2019 6.Lp trnh GUI trong Java
99/132
#Tabbed'ane
-
7/25/2019 6.Lp trnh GUI trong Java
100/132
To m0i Vi tHn" #Tabbed'ane
J8a''e)Pane ta''e)Pane V ne7 J8a''e)Pane*+
G-n th,m Tab m0i vo Vi tHn" #Tabbed'aneta''e)Pane.a))8a'*8a' nameb, icn, cmpnent, 8ltipb+
#Tabbed'ane
-
7/25/2019 6.Lp trnh GUI trong Java
101/132
#!plit'ane
-
7/25/2019 6.Lp trnh GUI trong Java
102/132
httpUmm!a'a$s+n$co>mdocsmboo"smt+toria7m+is9ingmIAa>p7Ismco>ponIntsm
#!plit'ane
-
7/25/2019 6.Lp trnh GUI trong Java
103/132
#!plit'ane #!croll'ane #Cist
-
7/25/2019 6.Lp trnh GUI trong Java
104/132
#!plit'ane #!croll'ane #Cist
-
7/25/2019 6.Lp trnh GUI trong Java
105/132
Mouse Event Handling
v'Intl7istInIr c:a >o+sI I'Ints
-
7/25/2019 6.Lp trnh GUI trong Java
106/132
EMou%eLi%tenerEMou%eMotionLi%tener
Fig. 12.16Mou%eLi%tenerandMou%eMotionLi%tenerinterface methods
-
7/25/2019 6.Lp trnh GUI trong Java
107/132
1 // Fi! "#!">$ Mou%eTracker!&a'a
2 // Hemon%tratin mou%e e'ent%!34 // Ja'a core (ackae%5 im(ort&a'a!a)t!*+6 im(ort&a'a!a)t!e'ent!*+78 // Ja'a exten%ion (ackae%9 im(ort&a'ax!%)in!*+
Mou%eTracker!&a'a
Lines 25-26
-
7/25/2019 6.Lp trnh GUI trong Java
108/132
( & 10
11(ublic cla%%Mou%eTracker extend%JFrame12 im(lement%Mou%eLi%tener. Mou%eMotionLi%tener ,1314 (ri'ateJLabel %tatu%Bar+1516 // %et u( 0I and rei%ter mou%e e'ent handler%17 (ublicMou%eTracker1218 ,19 %u(er1 3Hemon%tratin Mou%e 'ent%32+20
21 %tatu%Bar 5 ne)JLabel12+22 etContent6ane12!add1 %tatu%Bar. BorderLa4out!90T@2+2324 // a((lication li%ten% to it% o)n mou%e e'ent%25 addMou%eLi%tener1 thi%2+26 addMou%eMotionLi%tener1 thi%2+2728 %et9ie1 #>. "882+29 %etDi%ible1 true2+30
E3132 // Mou%eLi%tener e'ent handler%3334 // handle e'ent )hen mou%e relea%ed immediatel4 a-ter (re%%35 (ublic 'oidmou%eClicked1 Mou%e'ent e'ent 2
Line 35
`IgistIr JFrametorIcIi'I >o+sI I'Ints
Fn'o"Id 9hIn +sIr prIssIsand rI7IasIs >o+sI b+tton
36 ,
37 %tatu%Bar!%etText1 3Clicked at 3 e'ent!etK12 38 3. 3 e'ent!etN12 3G32+39 E4041 // handle e'ent )hen mou%e (re%%ed42 (ublic 'oidmou%e6re%%ed1 Mou%e'ent e'ent 243 ,44 %tatu%Bar!%etText1 36re%%ed at 3 e'ent!etK12
Mou%eTracker!&a'a
Line 42
Fn'o"Id 9hIn +sIrprIssIs >o+sI b+tton
-
7/25/2019 6.Lp trnh GUI trong Java
109/132
45 3. 3 e'ent!etN12 3G32+46
E4748 // handle e'ent )hen mou%e relea%ed a-ter drain49 (ublic 'oidmou%eRelea%ed1 Mou%e'ent e'ent 250 ,51 %tatu%Bar!%etText1 3Relea%ed at 3 e'ent!etK12 52 3. 3 e'ent!etN12 3G3 2+53 E5455 // handle e'ent )hen mou%e enter% area
56 (ublic 'oidmou%entered1 Mou%e'ent e'ent 257 ,58 J(tion6ane!%ho)Me%%aeHialo1 null. 3Mou%e in )indo)32+59 E6061 // handle e'ent )hen mou%e exit% area62 (ublic 'oidmou%exited1 Mou%e'ent e'ent 263 ,64 %tatu%Bar!%etText1 3Mou%e out%ide )indo)32+65
E6667 // Mou%eMotionLi%tener e'ent handler%6869 // handle e'ent )hen u%er dra% mou%e )ith button (re%%ed70 (ublic 'oidmou%eHraed1 Mou%e'ent e'ent 2
Line 49
Line 56
Line 62
Line 70
Fn'o"Id 9hIn +sIr rI7IasIs >o+sIb+tton a]tIr dragging >o+sI
Fn'o"Id 9hIn >o+sIc+rsor IntIrs JFrame
Fn'o"Id 9hIn >o+sIc+rsor IAits JFrame
Fn'o"Id 9hIn +sIrdrags >o+sI c+rsor
71 ,72 %tatu%Bar!%etText1 3Hraed at 3 e'ent!etK12 73 3. 3 e'ent!etN12 3G32+74 E7576 // handle e'ent )hen u%er mo'e% mou%e77 (ublic 'oidmou%eMo'ed1 Mou%e'ent e'ent 278 ,79 %tatu%Bar!%etText1 3Mo'ed at 3 e'ent!etK12
Mou%eTracker!&a'a
Line 77
Fn'o"Id 9hIn +sIr>o'Is >o+sI c+rsor
-
7/25/2019 6.Lp trnh GUI trong Java
110/132
79 %tatu%Bar!%etText1 Mo'ed at e'ent!etK12 80 3. 3 e'ent!etN12 3G32+
81 E8283 // execute a((lication84 (ublic %tatic 'oidmain1 9trin ar%G 285 ,86 Mou%eTracker a((lication 5 ne)Mou%eTracker12+8788 a((lication!%etHe-aultClo%e(eration189 JFrame!KIT:CL92+90 E9192 E // end cla%% Mou%eTracker
Mou%eTracker!&a'a
-
7/25/2019 6.Lp trnh GUI trong Java
111/132
1 // Fi! "#!#8$ Mou%eHetail%!&a'a
2 // Hemon%tratin mou%e click% and3 // di%tinui%hin bet)een mou%e button%!45 // Ja'a core (ackae%6 im(ort&a'a!a)t!*+7 im(ort&a'a!a)t!e'ent!*+89 // Ja'a exten%ion (ackae%10 i t & i *
Mou%eHetail%!&a'a
Line 21
-
7/25/2019 6.Lp trnh GUI trong Java
112/132
10 im(ort&a'ax!%)in!*+1112(ublic cla%%Mou%eHetail% extend%JFrame ,13 (ri'ate intx6o%. 46o%+1415 // %et title bar 9trin. rei%ter mou%e li%tener and %ie16 // and %ho) )indo)17 (ublicMou%eHetail%1218 ,19 %u(er1 3Mou%e click% and button%32+20
21 addMou%eLi%tener1 ne)Mou%eClick@andler12 2+2223 %et9ie1
-
7/25/2019 6.Lp trnh GUI trong Java
113/132
45
46 // inner cla%% to handle mou%e e'ent%47 (ri'ate cla%%Mou%eClick@andler extend%Mou%eAda(ter ,4849 // handle mou%e click e'ent and determine )hich mou%e50 // button )a% (re%%ed51 (ublic 'oidmou%eClicked1 Mou%e'ent e'ent 252 ,53 x6o% 5 e'ent!etK12+54 46o% 5 e'ent!etN12+5556 9trin title 557 3Clicked 3 e'ent!etClickCount12 3 time1%23+5859 // riht mou%e button60 i-1 e'ent!i%MetaHo)n12 261 title 5 3 )ith riht mou%e button3+6263 // middle mou%e button64 el%e i- 1 e'ent!i%AltHo)n12 2
65 title 53 )ith center mou%e button3+6667 // le-t mou%e button68 el%e69 title 5 3 )ith le-t mou%e button3+
Lines 53-54
Lines 56-57
Lines 60-61
Lines 64-65
Fn'o"I >Ithodmou%eClicked9hIn +sIr c7ic"s >o+sI
8torI >o+sIlc+rsor coordinatIs9hIrI >o+sI 9as c7ic"Id
DItIr>inI n+>bIr o] ti>Is+sIr has c7ic"Id >o+sI
DItIr>inI i] +sIr c7ic"Idright >o+sI b+tton
DItIr>inI i] +sIr c7ic"Id
>idd7I >o+sI b+tton
7071 %etTitle1 title 2+ // %et title bar o- )indo)72 re(aint12+73 E7475 E // end (ri'ate inner cla%% Mou%eClick@andler7677 E // end cla%% Mou%eHetail%
Mou%eHetail%!&a'a
-
7/25/2019 6.Lp trnh GUI trong Java
114/132
Keyboard Event Handling
FntIr]acI e4Li%tener6 DVng R A 7 0ey events
-
7/25/2019 6.Lp trnh GUI trong Java
115/132
6 DVng R A 7 0ey events
Jht sinh "hi # ph> c nh=n ') th ra$ e4'ent
6
1 // Fi! "#!##$ e4Hemo!&a'a
2 // Hemon%tratin ke4%troke e'ent%!34 // Ja'a core (ackae%5 im(ort&a'a!a)t!*+6 im(ort&a'a!a)t!e'ent!*+78 // Ja'a exten%ion (ackae%9 im(ort&a'ax!%)in!*+10
e4Hemo!&a'a
Line 28
Line35
-
7/25/2019 6.Lp trnh GUI trong Java
116/132
1011(ublic cla%% e4Hemo extend%JFrame im(lement%e4Li%tener ,12 (ri'ate9trin line" 5 33. line# 5 33+13 (ri'ate9trin line< 5 33+14 (ri'ateJTextArea textArea+1516 // %et u( 0I17 (ublice4Hemo1218 ,19 %u(er13Hemon%tratin e4%troke 'ent%32+20
21 // %et u( JTextArea22 textArea 5 ne)JTextArea1 "8. "2+23 textArea!%etText1 36re%% an4 ke4 on the ke4board!!!32+24 textArea!%etnabled1 -al%e2+25 etContent6ane12!add1 textArea 2+2627 // allo) -rame to (roce%% e4 e'ent%28 adde4Li%tener1 thi%2+2930 %et9ie1
-
7/25/2019 6.Lp trnh GUI trong Java
117/132
45 line" 5 3e4 relea%ed$ 3
46 e'ent!ete4Text1 e'ent!ete4Code12 2+47 %etLine%#and
-
7/25/2019 6.Lp trnh GUI trong Java
118/132
79 a((lication!%etHe-aultClo%e(eration1
80 JFrame!KIT:CL92+81 E8283 E // end cla%% e4Hemo
Pluggable Look And Feel
89ing h tr p7+ggab7I 7oo"landl]II7$
-
7/25/2019 6.Lp trnh GUI trong Java
119/132
89ing h tr 7oiU6\oti] l
wco>$s+n$!a'a$s9ing$p7a]$>oti]$\oti]Loo"/nd;II7w
60indo9s lwco>$s+n$!a'a$s9ing$p7a]$9indo9s$0indo9sLoo"/nd;II7w
6\Inta7 &5a'a p7at]or>( l
w!a'aA$s9ing$p7a]$>Ita7$\Ita7Loo"/nd;II7w
Thay i Look and Feel EF\anagIr$sItLoo"/nd;II7&8tring c7assPa>I( thro9s
Ens+pportIdLoo"/nd;II7vAcIption
-
7/25/2019 6.Lp trnh GUI trong Java
120/132
89ingEti7itiIs$+pdatIponIntTrIIEF&ponInt c(
trC
EF\anagIr$sItLoo"/nd;II7&
qco>$s+n$!a'a$s9ing$p7a]$\oti]$\oti]Loo"/nd;II7 (_ 89ingEti7itiIs$+pdatIponIntTrIIEF& >C;ra>I (_
catch& Ens+pportIdLoo"/nd;II7vAcIption I (
Look And Feel Demo
-
7/25/2019 6.Lp trnh GUI trong Java
121/132
#ToolEar Icon Ima"eIcon
-
7/25/2019 6.Lp trnh GUI trong Java
122/132
5Q+ttonF>agIFcon
#ToolEar Icon Ima"eIcon
=t tltip ch Xcn trAn thanh tl'ar
-
7/25/2019 6.Lp trnh GUI trong Java
123/132
p
Graphics Context v Object
Graphics contIAt6 W tr thao tc ' trn >)n hnh
-
7/25/2019 6.Lp trnh GUI trong Java
124/132
EVWi tXYn ra(hic%+n 7 graphics contIAt i*+ "hiRn cch '
-
7/25/2019 6.Lp trnh GUI trong Java
125/132
-
7/25/2019 6.Lp trnh GUI trong Java
126/132
6Pa>IU tn ]ont c trong h thng68tC7IU ;HPT$JL/FP ;HPT$FT/LF
-
7/25/2019 6.Lp trnh GUI trong Java
127/132
dra)Line1x". 4". x#. 4#26 Dra9 7inI ]ro> x" 4"to x# 4#
dra)Rect1x". 4". )idth. heiht26 Dra9s rIctang7I 9ith +ppIr 7I]t cornIr x". 4"
-illRect1x". 4". )idth. heiht26 /s abo'I IAcIpt ]i77s rIctang7I 9ith c+rrInt co7or
clearRect1x". 4". )idth. heiht2
6 /s abo'I IAcIpt ]i77s rIctang7I 9ith bac"gro+nd co7or
Lp Graphics
dra)
-
7/25/2019 6.Lp trnh GUI trong Java
128/132
-ill
-
7/25/2019 6.Lp trnh GUI trong Java
129/132
p+b7ic LinIs`IctsH'a7s&( s+pIr& wDra9ing 7inIs rIctang7Is and o'a7sw (_
sIt8iI& ~ #K (_
sItSisib7I&tr+I(_
p+b7ic 'oid paint& Graphics g ( g$sIt
-
7/25/2019 6.Lp trnh GUI trong Java
130/132
g & (_
g$sIt
-
7/25/2019 6.Lp trnh GUI trong Java
131/132
sho9Dia7og&ponInt parInt8tring tit7I
-
7/25/2019 6.Lp trnh GUI trong Java
132/132
8I7Ict a co7or ]ro> onI o] thI co7or s9atchIs$