Professional Documents
Culture Documents
(123doc - VN) - Lap Trinh Java Nang Cao Lap Trinh Giao Dien
(123doc - VN) - Lap Trinh Java Nang Cao Lap Trinh Giao Dien
JButton
!
Button l mt thnh phn gy ra mt
s kin hnh ng khi uoc kch
chut.
!
Cc constructor cua JButton:
JButton()
JButton(String text)
JButton(String text, Icon icon)
JButton(Icon icon)
C" th!" tnh JButton
!
text
!
icon
!
mnemonic
!
horizontalAlignment
!
verticalAlignment
!
horizontalTextPosition
!
verticalTextPosition
Using Buttons
1p ;ng "" +i Pin JButton
JButton bt = new JButton(OK);
bt.setActionCommand(Left);
bt.addActionListener(new Nghenut());
static class Nghenut implements ActionListener{
public void actionPerformed(ActionEvent e)
{
// Get the button label
String actionCommand = e.getActionCommand();
// Make sure the event source is Left button
if (e.getSource() instanceof JButton)
// Make sure it is the right button
if ("Left".equals(actionCommand))
System.out.println ("Button pressed");
}
JCheckBox
!
%hec! &o' l mt thnh phn cho php
ngui dung bt hay tt mt la chon,
ging nhu 1 cng tc n.
!
Cc constructor:
JCheckBox()
JCheckBox(String text)
JCheckBox(String text, boolean selected)
JCheckBox(Icon icon)
JCheckBox(String text, Icon icon)
JCheckBox(String text,Icon icon,boolean selected)
C" th!" tnh JCheckBox
!
JCheckBox c tt ca cc thuc tnh
trong JButton. Ngoi ta, JCheckBox
c thuc tnh:
selected
!
Using Check Box:
JCheckBox cb = new
JCheckBox(Chon);
if (cb.isSelected()) { . . . }
JRadioButton
!
Cc Radio &utton l s bin i cua cc check
box. Chng thung uoc su dng trong mt nhm khi
m ch c 1 button uoc chon tai mt thi im.
!
Cc constructor:
JRadioButton()
JRadioButton(String text)
JRadioButton(String text, boolean selected)
JRadioButton(Icon icon)
JRadioButton(String text, Icon icon)
JRadioButton(String text, Icon icon, boolean selected)
C" th!" tnh JRadioButton
JRadioButton c tt ca cc thuc tnh
trong JButton. Ngoi ra, JRadioButton
c thuc tnh:
selected
%p nhSm "" Radio Button
ButtonGroup btg = new ButtonGroup();
btg.add(jrb1);
btg.add(jrb2);
&+ing Tadio B!tton+:
JRadioButton rb1 = new JRadioButton(Chon);
JRadioButton rb2 = new JRadioButton(Khong
chon);
btg.add(rb1); btg.add(rb2);
if (rb1.isSelected()) { . . . }
JLae!
!
Label dng 8: hi:n th9 mt "h!hi *Dn M=n thng thWng
nhtm m t= thm thng tin "ho "" 8@i t2ng Ph",
!
C" "on+tr!"tor "`a 0LaMJ5:
JLabel()
JLabel(String text)
JLabel(String text,int hAlignment)
JLabel(Icon icon)
JLabel(Icon icon, int hAlignment)
JLabel(String text,Icon icon,int hAlignment)
C"c thu#c t$nh JLae!
!
text
!
icon
!
horizontalAlignment
!
verticalAlignment
Using Labels
J%ext&ie!d
!
Text field l nhp d liu dang van
ban trn 1 dng.
!
Cc constructor cua JTextField:
JTextField()
JTextField(int columns)
Tao mt text field trng c s ct xc inh.
JTextField(String text)
Tao mt text field vi van ban c sn.
JTextField(String text, int columns)
Tao mt text field vi van ban c sn v s
ct xc inh.
C" th!" tnh J%ext&ie!d
!
text
!
horizontalAlignment
!
editable
!
columns
C" phng th;" J%ext&ie!d
!
getText()
Tra v chui k t trong text field.
!
setText(String text)
at chui k t trong text field.
!
setEditable(boolean editable)
Cho php hoac v hiu ha soan thao trong
text field. Mac inh, editable l true.
!
setColumns(int)
Thit lp s ct trong text field. Chiu
di cua text field c th thay i.
J%ext'rea
!
TextArea l khung cho php ngui su
dng nhp vo nhiu dng van ban.
!
Cc constructor cua JTextArea:
JTextArea()
JTextArea(String s)
JTextArea(int rows, int columns)
JTextArea(String s, int rows, int columns)
C" th!" tnh J%ext'rea
!
text
!
editable
!
columns
!
lineWrap
!
wrapStyleWord
!
rows
!
lineCount
!
tabSize
(X d<ng %ext 'rea
Chung trnh hin thi 1 anh v 1 title trong
1 label, hin thi van ban trong text area.
IJ+"riptionOanJ5
YN5M5'magJ
YN5M5Tit5J
YNtaTJ7tIJ+"ription
l+Jt'magJ'"on
l+JtTit5J
l+JtTJ7tIJ+"ription
lgJtAinim!m(ioJ
1
1
TJ7t-rJaIJmo
0OanJ5
Y"har toPJn
lgJtToPJn
l+JtToPJn
lpaintComponJt
lmo!+JC5i"PJd
0KramJ
Y"har toPJn
lgJtToPJn
l+JtToPJn
lpaintComponJt
lmo!+JC5i"PJd
JComoBox
!
%om&o &o' l danh sch n gian cc mc
chon. C ban n thc hin chc nang ging
nhu 1 list, nhung ch c th ly 1 gi
tri.
!
Cc constructor:
JComboBox()
tao 1 combo box rng
JComboBox(Object[] stringItems)
tao 1 combo box cha cc phn tu
trong dy
C" phng th;" JComoBox
jcbo.addItem(Object item)
thm 1 mc chon vo JComboBox jcbo
jcbo.getItem()
jcbo.getItemAt(int index)
ly 1 mc chon tu JComboBox jcbo
jcbo.removeItemAt(int index)
loai 1 mc chon khoi JComboBox jcbo
Using Combo Box:
(X d<ng item(tateChanged Handler
?hi mt 5ia "han 82" "hJ"P ho]" !n"hJ"PC
itemStateChanged() "ho ItemEvent *#
actionPerformed() hand5Jr "ho ActionEvent
+g 82" gai,
public void itemStateChanged(ItemEvent e){
// Make sure the source is a combo box
if (e.getSource() instanceof JComboBox)
String s = (String)e.getItem();
}
JLi)t
!
List l mt thnh phn c ban thc hin
chc nang ging combo box, nhung n cho
php ngui su dng chon mt hoac nhiu gi
tri.
!
Cc constructor:
JList()
tao 1 list rng.
JList(Object[] stringItems)
tao 1 list cha cc phn tu trong dy.
C" th!" tnh JLi)t
!
selectedIndex
!
selectedIndices
!
selectedValue
!
selectedValues
!
selectionMode
!
visibleRowCount
Using Lists:
J(cro!!Bar
!
(crollBar l mt iu khin cho php
ngui su dng chon tu mt dai cc gi
tri.
!
Cc constructor:
JScrollBar()
JScrollBar(int orientation)
JScrollBar(int orientation, int value,
int extent, int min, int max)
C" th!" tnh J(cro!!Bar
!
orientation: 1 - doc, 0 - ngang
!
maximum, minimum
!
visibleAmount (extent): rng cua phn
con chay
!
value: gi tri hin thi cua scroll bar
!
blockIncrement: gi tri uoc cng thm
khi kch hoat vung tang.
!
unitIncrement: gi tri uoc cng thm khi
kch hoat u tang.
C" phng th;" J(cro!!Bar
!
setBlockIncrement(int increment)
!
setMaximum(int maximum)
!
setMinimum
!
setOrientation(int orientation)
!
setUnitIncrement(int increment)
!
setValue(int value)
!
setVisibleAmount (extent):
!
getBlockIncrement()
!
...
Borders
!
Ban c th thit lp mt border trn bt
ky i tuong no cua lp JComponent, nhung
thung hu ch khi thit lp mt titled
border trn JPanel nhm mt tp cc thnh
phn giao din ngui su dng c lin quan.
!
Using border:
C" phng th;" tnh 8: tBo Borders
!
createTitledBorder(String title)
!
createLoweredBevelBorder()
!
createRaisedBevelBorder()
!
createLineBorder(Color color)
!
createLineBorder(Color color, int thickness)
!
createEtchedBorder()
!
createEtchedBorder(Color highlight,
Color shadow, boolean selected)
!
createEmptyBorder()
!
createMatteBorder(int top, int left,
int bottom, int right, Icon tileIcon)
!
createCompoundBorder(Border outsideBorder,
Border insideBorder)
Dialogs
!
C th su dng lp JOptionPane
tao 4 loai dialog chun:
/
Message Dialog hi:n th9 mt mJ++agJ *# 82i ngWi +X
d<ng P"h nZt L? 8: 8Sng hp thoBi,
/
Confirmation Dialog hi:n th9 "! hi *# 8v ngh9 ngWi
+X d<ng tr= 5WiC *d: L? hay Can"J5
/
Input Dialog hi:n th9 "! hi *# nhn d^ 5i! *#o t 1 tJ7t
6iJ5dC "omMo Mo7 ho]" 5i+t,
/
Option Dialog hi:n th9 "! hi *# nhn "! tr= 5Wi t mt
tp "" 5ia "han,
TBo "" Message Dialog
Su dng phung thc tnh trong lp
JOptionPane
showMessageDialog(Component parentComponent,
Object message)
showMessageDialog(Component parentComponent, Object
message,String title, int messageType)
showMessageDialog(Component parentComponent,
Object message, String title,
int messageType, Icon icon)
TBo "" Confirmation Dialog
Su dng phung thc tnh trong lp
JOptionPane
showConfirmDialog(Component parentComponent,
Object message)
showConfirmDialog(Component parentComponent, Object
message, String title, int optionType)
showConfirmDialog(Component parentComponent, Object
message, String title, int optionType,
[[int messageType], Icon icon])
TBo "" Option Dialog
Su dng phung thc tnh trong lp
JOptionPane
showOptionDialog(Component parentComponent, Object
message, String title, int optionType, int
messageType, Icon icon, Object[] options, Object
initialValue)
Su dng cc dialog:
Menus
!
Java cung cp mt s lp - JMenuBar,
JMenu, JMenuItem, JCheckBoxMenuItem, v
JRadioButtonMenuItem - thc thi menu
trong mt frame.
!
Mt JFrame hoac JApplet c th cha mt
menu &ar trn c gn cc pull$do)n
menu. Cc menu cha cc menu item
ngui dung la chon (hoac bt/tt).
Menu bar c th uoc xem nhu mt cu
trc h tro cc menu.
Menu Demo
L4p J*enuBar
Menu bar "h;a "" mJn!x mJn! Mar "h "S th: 82"
thm *#o 1 6ramJ, 1oBn "odJ +a! tBo *# thm mt
0AJn!Bar *#o 1 6ramJ:
JFrame f = new JFrame();
f.setSize(300, 200);
f.setVisible(true);
JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
L4p *enu
BBn g3n "" mJn! *#o mt JMenuBar, 1oBn "odJ
+a! tBo > mJn! File *# elpC *# thm "hZng *#o
JMenuBar mb:
JMenu fileMenu = new JMenu("File", false);
JMenu helpMenu = new JMenu("elp", true);
mb.add(fileMenu);
mb.add(helpMenu);
L4p J*enu+tem
1oBn "odJ +a! thm "" m<" "han GmJn! itJmH *# ""
+Jparator trong mJn! fileMenu:
fileMenu.add(new JMenuItem("New"));
fileMenu.add(new JMenuItem("Open"));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem("Print"));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem("Exit"));
Submenus
BBn "S th: thm "" +!MmJn!+ *#o "" mJn! itJm, 1oBn
"odJ +a! thm "" +!MmJn! &ni7C TC *# .infk *#o
trong m<" "han (o6t)arJ,
JMenu softwareelpSubMenu = new JMenu("Software");
JMenu hardwareelpSubMenu = new JMenu("ardware");
helpMenu.add(softwareelpSubMenu);
helpMenu.add(hardwareelpSubMenu);
softwareelpSubMenu.add(new JMenuItem("Unix"));
softwareelpSubMenu.add(new JMenuItem("NT"));
softwareelpSubMenu.add(new JMenuItem("Win5"));
Submenu Demo
(X d<ng Menu
Tao mt giao din thc hin cc php
ton s hoc gia 2 s Number1 v
Number2. Giao din cha cc nhn v
text field cho Number 1, Number 2,
v Result (su dng nhu i vi
JButton).
B4" 1: TBo 1 +!M"5a++ "`a 54p JFrame G82" gai
5# 1 SubFrameH 8: 7" 89nh "Xa +s m4i 5#m *i"
g, e d<C tFt "= "" "hng trnh ;ng d<ng %&' m_
rng JFrame *# 5# "" +!M"5a++ "`a JFrame,
TBo thm Window Y M4" 1
TBo thm Window Y M4" >
Buc 2: Tao 1 instance cua SubFrame
trong ng dng hoac trong applet.
V d:
SubFrame subFrame = new
SubFrame("SubFrame Title");
TBo thm Window Y M4" \
Buc 3: Tao 1 JButton kch hoat
subFrame.
add(new JButton("Activate SubFrame"));
TBo thm Window Y M4"
Buc 4: chng phung thc actionPerformed() nhu
sau:
public actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (e.target instanceof Button)
{
if ("Activate
SubFrame".equals(actionCommand))
{
subFrame.setVisible(true);
}
}
}
e d<: TBo nhiv! Window
V d tao 1 main window c 1 text area
trong scroll pane, 1 button "Show
istogram". Khi ngui su dng kch vo
button, 1 cua s mi xut hin hin
thi biu cho biu din tn s xut
hin cua cc k t trong text area.
J(cro!!Pane
Scroll pane l mt thnh phn t
ng h tro cun cua s m khng cn
lp trnh.
Using Scroll Pane: add mt TextArea
vo trong mt Scroll pane
CF! trZ" Scroll Pane
Co5!mn hJadJr CornJr
ComponJnt
To) hJadJr
0eiJ)port
("ro55aM5J ComponJnt
Qorioonta5 +"ro55 Mar+
eJrti"a5 +"ro55 Mar+
CornJr
ComponJnt
CornJr
ComponJnt
CornJr
ComponJnt
J%aedPane
Tabbed pane cung cp mt tp cc tab loai
tru ln nhau truy nhp nhiu thnh
phn.
Using Tabbed Pane: