Escolar Documentos
Profissional Documentos
Cultura Documentos
p
p
t
t
r
r
n
n
h
h
d
d
h
h
a
a
v
v
o
o
i
i
A
A
W
W
T
T
CAO Duc Thong Thanglong University
thongcd@thanglong.edu.vn
2
N
N
i
i
d
d
u
u
n
n
g
g
Tcng quan ve AWT
Cc thnh phn AWT
Quan l trnh by
X l Su kien
3
T
T
h
h
u
u
v
v
i
i
n
n
A
A
W
W
T
T
AWT l viet tt ca Abstract Windowing Toolkit
AWT cho php tao cc thnh phn dc hoa
AWT cho php nhn du lieu t chut, bn phm
Cc thnh phn dc hoa cc ban ca AWT
vt cha (Container)
Thnh phn (Component)
Trnh quan l trnh by (Layout Manager)
Dc hoa (Graphics), phng chu (Font), su kien (Event)
4
T
T
h
h
u
u
v
v
i
i
n
n
A
A
W
W
T
T
Object Color Event Font Graphics
Image
Insets
Polygon
Rectangle
Toolkit MenuComponent
XXXLayout
CheckboxGroup
Component
MenuBar
MenuItem
Menu
Checkbox
MenuItem
5
T
T
h
h
u
u
v
v
i
i
n
n
A
A
W
W
T
T
Component
Button
Canvas
AWTException AWTError
Applet
Checkbox
Choice
Container
Label
List
Scrollbar
TextComponent
Panel
Window
ScrollPane
TextArea
TextField
Frame
Dialog
FileDialog
6
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Component (thnh phn)
L mt dci tucng dc hoa c the hien thj ducc trn mn hnh v
c the tucng tc vci nguci dng
L mt abstract superclass cho hu het cc component ca AWT
V du ve Component: button, checkbox, scrollbar
Nt sc phucng thc cc ban ca Component
getBackGround(): tra ve mu nen ca Component
getBounds(): tra ve pham vi ca Component (Rectangle)
getFont(): tra ve font hien tai ca Component
getForeGround(): tra ve mu ve ca Component
getHeight(): tra ve chieu cao ca Component (pixel, kieu int)
7
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Nt sc phucng thc cc ban ca Component
getSize(): tra ve kch thucc ca Component (Dimenstion)
getWidth(): tra ve chieu rng ca Component (int)
getX(), getY(): tra ve toa d hien tai
isEnable(): boolean
paint(Graphics): chju trch nhiem hien thj component
repaint(): ducc goi de ve lai giao dien cho component
setVisible(boolean): hien thj component
8
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Container (vt cha)
Cha trong gi java.awt
L vng c the dt cc thnh phn giao dien
Nt sc loai Container: Panel, Frame, Dialog
C mt Component c kha nng cha cc Component khc
De thm mt Component vo Container ta s dung phucng thc
add(Component)
Container s dung mt layout manager de sp xep cc
Component
9
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Frame
Tha ke t Window nn cung l mt Container
Frame va l Component va l Container
Tao Frame
Frame()
Frame(String title)
V du
10
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Frame
import java.awt.*;
public class UseLessFrame extends Frame {
public UseLessFrame(){
super("Useless Frame");
setSize(300,200);
setVisible(true);
}
public static void main(String[] args) {
UseLessFrame frame =new UseLessFrame();
}
}
11
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Frame
12
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
Su kien l g
Khi nguci dng thuc hien mt hnh dng trn GUI , mt su kien
se ducc sinh ra.
Cc su kien l cc dci tucng m ta nhung g d xay ra
Nci tc dng ca nguci dng se tao ra nhung loai su kien khc
nhau
Kch chut
Button
ActionEvent
EventHandler
actionPerformed(ActionEvent e){
//Do something
}
13
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
Event Sources
L ngucn sinh ra su
kien.
Su kien ducc sinh ra duci dang mt object, v du ActionEvent.
Event Handlers
Event handler l mt phucng thc, n nhn ve mt dci tucng
event, giai m v x l cc tucng tc vci nguci dng
Lcp cha cc phucng thc (event handler) ducc goi l lcp nghe
su kien (listener)
De mt lcp nghe c the x l mt su kien no d, ngucn sinh ra
su kien cn phai dng k lcp nghe
Nt ngucn sinh ra su kien c the c nhieu lcp nghe
Nt lcp mucn l listener phai implement mt giao tiep thch hcp
14
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
V du ve x l su kien
import java.awt.*;
import java.awt.event.*;
class EventTest extends Frame implements ActionListener {
Label lab =new Label("Enter a number");
TextField tf1 =new TextField(5);
TextField tf2 =new TextField(5);
Button btnResult =new Button("Double is");
Button ext =new Button("exit");
public EventTest(String title) {
super(title);
setLayout(new FlowLayout());
btnResult.addActionListener(this);
ext.addActionListener(this);
add(lab); add(tf1); add(btnResult); add(tf2); add(ext);
}
15
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
V du ve x l su kien
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() ==btnResult) {
int num =Integer.parseInt(tf1.getText()) * 2;
tf2.setText(String.valueOf(num));
}
if (ae.getSource() ==ext) {
System.exit(0);
}
}
public static void main(String args[]) {
EventTest t =new EventTest("Event handling");
t.setSize(300, 200);
t.setVisible(true);
}
}
16
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
V du ve x l su kien
17
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
Cc loai su kien (Event)
java.util.EventObject
java.awt.AWTEvent
ActionEvent
java.awt.event
AdjustmentEvent
ComponentEvent
ItemEvent
FocusEvent
InputEvent
ContainerEvent
KeyEvent
MouseEvent
TextEvent WindowEvent
18
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
M ta su kien
19
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
Cc loai Listener
ActionListener
AdjustmentListener
E
v
ContainerListener
e
n
FocusListener
t
L
i
ItemListener
s
t
KeyListener
e
n
MouseListener
e
r
MouseMotionListener
TextListener
WindowListener
20
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
Dng k dci tucng nghe
add + loai su kien + Listener( su kien)
V du vci nt Button
addActionListener(ActionListener)
V du vci danh sch
List
addActionListener(A
ctionListener)
addI temListener(I te
mListener)
21
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
Ci dt quan l su kien
Xc djnh dci tucng se gy ra su kien (source)
Xc djnh su kien c the xy ra tucng ng vci dci tucng m ta cn
quan l (object)
Xc djnh dci tucng nghe (listener) v ci dt cc phucng thc
tucng ng
Dng k dci tucng nghe cho dci tucng gy ra su kien
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
V du ve quan l su
kien
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestButton {
private Frame f;
private Button b;
public TestButton(){
f =new Frame("Test");
b =new Button("Press me");
b.setActionCommand("ButtonPressed");
}
public void init(){
b.addActionListener(new ButtonHandler());
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
}
22
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
23
V du ve quan l su
kien
public static void main(String[] args){
TestButton test =new TestButton();
test.init();
}
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Button's command is: "+
e.getActionCommand());
}
}
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
24
V du ve quan l su
kien
Button's command is: ButtonPressed
Button's command is: ButtonPressed
Button's command is: ButtonPressed
25
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Frame dng de test cc thnh phn khc
import java.awt.*;
import java.awt.event.*;
public class ComponentTestFrame extends Frame implements
WindowListener {
public ComponentTestFrame(String title){
super(title);
setBackground(SystemColor.control);
setSize(400,300);
setLocation(200,150);
setLayout(new FlowLayout());
addWindowListener(this);
}
26
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Frame dng de test cc thnh phn khc
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}
27
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Nt sc phucng thc ca Frame
Frame()
Frame(String)
I mage getI conI mage()
MenuBar getMenuBar()
String getTitle()
Boolean isResizeable()
setI conI mage(I mage)
setMenuBar(MenuBar)
setTitle(String)
setVisible(boolean)
28
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
GUI Frame
import java.awt.*;
import java.awt.event.*;
public class GUIFrame extends Frame {
public GUIFrame(String title){
super(title);
setBackground(SystemColor.control);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
29
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
GUI Frame
public void setVisible(boolean visible){
if(visible){
Dimension d =
Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getWidth())/2, (d.height
-getHeight())/2);
}
super.setVisible(visible);
}
public static void main(String[] args){
GUIFrame frame =new GUIFrame("GUI Frame");
frame.setSize(400,300);
frame.setVisible(true);
}
}
30
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
GUI Frame
31
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Label
Dng de hien thj mt dcan vn ban trong mt Container
Cc phucng thc khct tao
Label()
Label(String text)
Label(String text, alignment): alignment c the nhn cc gi
trj Label.LEFT, Label.RIGHT, Label.CENTER
Phucng thc khc
setFont(Font f)
setText(String s)
getText()
getAlignment()
32
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Label
import java.awt.*;
public class LabelTest {
public LabelTest() {
Label l1 =new Label("Label");
Label l2 =new Label("I am a label");
l2.setFont(new Font("Timesroman", Font.BOLD, 18));
Label l3 =new Label();
l3.setText("I am disable");
l3.setEnabled(false);
Label l4 =new Label("Colored, right aligned", Label.RIGHT);
l4.setForeground(Color.green);
l4.setBackground(Color.black);
ComponentTestFrame frame = new
ComponentTestFrame("Label Test");
33
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Label
frame.add(l1);
frame.add(l2);
frame.add(l3);
frame.add(l4);
frame.setVisible(true);
}
public static void main(String[] args) {
LabelTest lt =new LabelTest();
}
}
34
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
TextComponent
L lcp cha ca TextField v TextArea
Nt sc phucng thc ca TextComponent
getCaretPosition()
getSelectedText()
getSelectionStart()
getSelectionEnd()
getText(), setText()
select(int, int)
setCaretPosition(int)
setEditable(boolean)
setSelectionStart(int)
setSelectionEnd(int)
35
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
TextField
Chi cha mt dng vn ban
Nt sc phucng thc
TextField()
TextField(int columns)
TextField(String s)
TextField(String s, int columns)
addActionListener(ActionListener)
echoCharI sSet()
setEchoChar(char)
setText()
setColumn(int)
36
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
TextField
Nt sc phucng thc
setEditable(boolean): dt che dTextField c soan thao ducc
hay khng
isEditable(): xc djnh xem c c che d Editable khng
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
TextField
import java.awt.*;
public class TextFieldTest {
public TextFieldTest() {
super();
TextField tf1 =new TextField();
TextField tf2 =new TextField(25);
tf2.setText("Type stuff here");
tf2.setFont(new Font("Timesroman",Font.BOLD,18));
TextField tf3 =new TextField("I am disabled",15);
tf3.setEnabled(false);
TextField tf4 =new TextField("Colors");
tf4.setBackground(Color.BLACK);
tf4.setForeground(Color.WHITE);
TextField tf5 =new TextField("Not editable");
tf5.setEditable(false);
TextField tf6 =new TextField("I am selected text !!!");
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
tf6.select(5, 13);
37
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
38
TextField
TextField tf7 =new TextField("Caret here --><--");
TextField tf8 =new TextField("username",8);
TextField tf9 =new TextField("password",8);
tf9.setEchoChar('*');
ComponentTestFrame frame = new
ComponentTestFrame("TextField Test");
frame.add(tf1); frame.add(tf2); frame.add(tf3);
frame.add(tf4); frame.add(tf5); frame.add(tf6);
frame.add(tf7); frame.add(tf8); frame.add(tf9);
frame.setVisible(true);
tf7.setCaretPosition(14);
}
public static void main(String[] args) {
TextFieldTest test =new TextFieldTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
39
TextField
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
40
TextArea
Hien thj vn ban c nhieu hcn mt dng
Nci TextArea c mt Scrollbar
Nt sc phucng thc khci tao
TextArea()
TextArea(int rows, int columns)
TextArea(String text)
TextArea(String text, int rows, int columns)
TextArea(String text, int rows, int columns, int ScrollType)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
41
TextArea
Nt sc phucng thc thucng dng
setText/getText
get/set row/column
setEditable/isEditable
append(String)
insert(String s, int i): chn chuci vo mt vj tr
replaceRange(String, int, int): thay the vn ban nm giua vj tr
int v int cho trucc
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
42
TextArea
import java.awt.*;
public class TextAreaTest {
public TextAreaTest() {
super();
TextArea ta1 =new TextArea(10,20);
TextArea ta2 =new TextArea("Text Area\n with
color",10,10,TextArea.SCROLLBARS_NONE);
ta2.setFont(new Font("Timesroman",Font.ITALIC,12));
ta2.setBackground(Color.BLACK);
ta2.setForeground(Color.GREEN);
TextArea ta3 =new TextArea("This textarea is not
editable",
10,15,TextArea.SCROLLBARS_HORIZONTAL_ONLY);
ta3.setEditable(false);
TextArea ta4 =new TextArea("This textarea is not enable",
4,25,TextArea.SCROLLBARS_NONE);
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
43
TextArea
}
ta4.setEditable(false);
ComponentTestFrame frame = new
ComponentTestFrame("TextArea Test");
frame.add(ta1); frame.add(ta2);
frame.add(ta3); frame.add(ta4);
frame.setVisible(true);
public static void main(String[] args) {
TextAreaTest test =new TextAreaTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
44
TextArea
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
45
Button
Tucng tc vci nguci dng
Thuc hien mt hnh dng no d khi nguci dng nhn nt
Nt sc phucng thc
Button()
Button(String text)
addActionListener(ActionListener)
String getLabel()
setLabel(String)
removeActionListener(ActionListener)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
46
Button
import java.awt.Frame;
import java.awt.*;
public class ButtonTest {
public ButtonTest() {
Button b1 =new Button("Button");
Button b2 =new Button();
b2.setLabel("Press me!");
b2.setFont(new Font("Timesroman", Font.BOLD, 16));
Button b3 =new Button("Can't press me");
b3.setEnabled(false);
Button b4 =new Button("Colors");
b4.setForeground(Color.green);
b4.setBackground(Color.black);
ComponentTestFrame frame = new
ComponentTestFrame("Button Test");
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
47
Button
}
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(b4);
frame.setVisible(true);
public static void main(String[] args) {
ButtonTest lt =new ButtonTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
48
Checkbox v RadioButton
Checkbox cho php nguci dng chon mt hay nhieu ty chon
C the chon nhieu option ca Checkbox
RadioButton cung gicng nhu Checkbox nhung chi cho php chon
mt option tai mt thci diem
Thnh phn Checkbox c the dng mt lcp phu (CheckboxGroup
de tao RadioButton)
Nt sc phucng thc
Checkbox()
Checkbox(String)
Checkbox(String, boolean)
Checkbox(String, boolean, CheckboxGroup)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
49
Checkbox v RadioButton
Nt sc phucng thc
addI temListener(I temListener)
set/getCheckboxGroup()
set/getLabel()
set/getState()
removeI temListener(I temListener)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
50
Checkbox v RadioButton
import java.awt.Checkbox;
public class CheckboxTest {
public CheckboxTest() {
super();
Checkbox cb1 =new Checkbox("J ava",false);
Checkbox cb2 =new Checkbox("C++",false);
cb2.setEnabled(false);
Checkbox cb3 =new Checkbox("HTML",true);
Checkbox cb4 =new Checkbox();
cb4.setLabel("Pascal"); cb4.setState(false);
ComponentTestFrame frame = new
ComponentTestFrame("CheckboxTest");
frame.add(cb1); frame.add(cb2);
frame.add(cb3); frame.add(cb4);
frame.setVisible(true);
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
51
Checkbox v RadioButton
public static void main(String[] args) {
CheckboxTest test =new CheckboxTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
52
Checkbox v RadioButton
import java.awt.*;
public class CheckboxGroupTest {
public CheckboxGroupTest() {
super();
CheckboxGroup group =new CheckboxGroup();
Checkbox cb1 =new Checkbox("J ava",false,group);
Checkbox cb2 =new Checkbox("C++",false,group);
cb2.setEnabled(false);
Checkbox cb3 =new Checkbox("HTML",true,group);
Checkbox cb4 =new Checkbox("",true,group);
cb4.setLabel("Pascal");
ComponentTestFrame frame = new
ComponentTestFrame("CheckboxGroupTest");
frame.add(cb1); frame.add(cb2);
frame.add(cb3); frame.add(cb4);
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
53
Checkbox v RadioButton
frame.setVisible(true);
}
public static void main(String[] args) {
CheckboxGroupTest test =new CheckboxGroupTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
54
Choice
Cho php lua chon trn mt danh sch cc thnh phn
Nt sc phucng thc
Choice()
add(String)
addI tem(String)
addI temListener(I temListener)
getI tem(int)
getI temCount()
getSelectedI ndex()
insert(String, int)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
55
Choice
Nt sc phucng thc
remove(int)
remove(String)
removeAll()
removeI temListener(I temListener)
select(int)
select(String)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
56
Choice
import java.awt.*;
public class ChoiceTest {
public ChoiceTest() {
super();
Choice c1 =new Choice();
c1.add("Soup");
c1.add("Salad");
Choice c2 =new Choice();
c2.add("J ava");
c2.add("C++");
c2.add("HTML");
c2.add("J avaScript");
c2.add("ADA");
Choice c3 =new Choice();
c3.add("One");
c3.add("Two");
c3.add("Three");
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
57
Choice
c3.setFont(new Font("Timesroman",Font.BOLD,18));
Choice c4 =new Choice();
c4.add("Computer");
c4.setEnabled(false);
ComponentTestFrame frame = new
ComponentTestFrame("ChoiceTest");
frame.add(c1);
frame.add(c2);
frame.add(c3);
frame.add(c4);
frame.setVisible(true);
}
public static void main(String[] args) {
ChoiceTest test =new ChoiceTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
58
Choice
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
59
List
Gicng nhu Choice nhung cho php hien thj nhieu lua chon cng
lc
Nguci dng c the lua chon nhieu item
Tham khao API Documentation
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
60
Canvas
L khu vuc cho php hien thj hnh anh hoc nhn ve cc su kien
ca nguci dng
Mucn ve bt c th g trn Canvas ta phai ci dt lai phucng thc
paint(Graphics)
V du
import java.awt.*;
public class CanvasTest extends Canvas {
public void paint(Graphics g){
setFont(new Font("Arial", Font.BOLD +Font.ITALIC, 16));
g.drawString("Canvas", 15, 25);
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
61
Canvas
public static void main(String[] args) {
CanvasTest c1 =new CanvasTest();
c1.setSize(100,100);
CanvasTest c2 =new CanvasTest();
c2.setSize(100,100);
c2.setBackground(Color.orange);
c2.setForeground(Color.blue);
CanvasTest c3 =new CanvasTest();
c3.setSize(200,50);
c3.setBackground(Color.white);
ComponentTestFrame frame = new ComponentTestFrame
("Canvas Test");
frame.add(c1);frame.add(c2);frame.add(c3);
frame.pack();
frame.setVisible(true);
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
62
Tcng quan ve Menu
Menu
MenuBar
Separator
MenuItem
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
63
Tcng quan ve Menu
Tao MenuBar
MenuBar menuBar = new MenuBar();
Cn MenuBar vo ca sc
myFrame.setMenuBar(menuBar);
Tao Menu
Menu fileMenu = new Menu(File);
Cn Menu vo MenuBar
menuBar.add(fileMenu);
Tao MenuI tem
MenuI tem fileOpen = new MenuI tem(Open);
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
64
Tcng quan ve Menu
Cn MenuI tem vo Menu
fileMenu.add(fileOpen);
Tao ducng phn cch
fileMenu.addSeparator();
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
65
MenuI tem
MenuI tem()
MenuItem(String)
MenuItem(String, MenuShortcut)
addActionListener(ActionListener)
set/getLabel();
get/setShortcut(MenuShortcu
t)
isEnabled()
setEnabled()
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
66
V du Menu
import java.awt.*;
import java.awt.event.KeyEvent;
public class MenuTest {
public MenuTest() {
super();
MenuItem fileNew =new MenuItem("New");
fileNew.setShortcut(new
MenuShortcut(KeyEvent.VK_N));
MenuItem fileOpen =new MenuItem("Open");
fileOpen.setShortcut(new
MenuShortcut(KeyEvent.VK_O));
MenuItem fileSave =new MenuItem("Save");
fileSave.setShortcut(new
MenuShortcut(KeyEvent.VK_S));
fileSave.setEnabled(false);
MenuItem fileSaveAs =new MenuItem("Save As");
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
67
V du Menu
fileSaveAs.setShortcut(new
MenuShortcut(KeyEvent.VK_A));
fileSaveAs.setEnabled(false);
MenuItem fileExit =new MenuItem("Exit");
fileExit.setShortcut(new
MenuShortcut(KeyEvent.VK_X));
MenuItem editCut =new MenuItem("Cut");
editCut.setShortcut(new
MenuShortcut(KeyEvent.VK_X));
MenuItem editCopy =new MenuItem("Copy");
editCopy.setShortcut(new
MenuShortcut(KeyEvent.VK_C));
MenuItem editPaste =new MenuItem("Paste");
editPaste.setShortcut(new
MenuShortcut(KeyEvent.VK_P));
Menu file =new Menu("File");
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
V du Menu
}
file.add(fileNew);file.add(fileOpen);
file.add(fileSave);file.add(fileSaveAs);
file.addSeparator();file.add(fileExit);
Menu edit =new Menu("Edit");
edit.add(editCut); edit.add(editCopy);
edit.add(editPaste);
MenuBar menuBar =new MenuBar();
menuBar.add(file); menuBar.add(edit);
ComponentTestFrame frame = new
ComponentTestFrame("MenuTest");
frame.setMenuBar(menuBar);
frame.setVisible(true);
public static void main(String[] args) {
MenuTest test =new MenuTest();
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
}
68
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
69
V du Menu
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
70
PopupMenu
L lcp con ca Menu
Dci tucng PopupMenu c the hien thj m khng cn MenuBar
C the hien thj c vj tr bt ky theo yu cu
import java.awt.*;
import java.awt.event.KeyEvent;
public class PopupMenuTest {
public PopupMenuTest() {
super();
MenuItem cut =new MenuItem("Cut");
cut.setShortcut(new MenuShortcut(KeyEvent.VK_X));
MenuItem copy =new MenuItem("Copy");
copy.setShortcut(new
MenuShortcut(KeyEvent.VK_C));
MenuItem paste =new MenuItem("Paste");
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
71
PopupMenu
paste.setShortcut(new
MenuShortcut(KeyEvent.VK_P));
MenuItem delete =new MenuItem("Delete");
PopupMenu popupMenu =new
PopupMenu("Clipboard");
popupMenu.add(cut);popupMenu.add(copy);
popupMenu.add(paste);popupMenu.add(delete);
ComponentTestFrame frame = new
ComponentTestFrame("PopupMenuTest");
frame.add(popupMenu);frame.setVisible(true);
popupMenu.show(frame,50,50);
}
public static void main(String[] args) {
PopupMenuTest test =new PopupMenuTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
72
PopupMenu
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
73
Panel
L mt Container nhung khng the tcn tai dc lp
Phai gn Panel vo mt Frame
Dng Panel de nhm cc thnh phn GUI lai vci nhau
import java.awt.*;
public class PanelTest {
public PanelTest() {
super();
Label lb1 =new Label("URL: ");
TextField tf1 =new TextField("",20);
Button b1 =new Button("Go");
Panel p1 =new Panel();
p1.setBackground(Color.CYAN);
p1.add(lb1); p1.add(tf1); p1.add(b1);
CheckboxGroup group =new CheckboxGroup();
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
Panel
Checkbox cb1 =new Checkbox("J ava",group,true);
Checkbox cb2 =new Checkbox("C++",group,false);
Checkbox cb3 =new Checkbox("HTML",group,false);
Checkbox cb4 =new Checkbox("ADA",group,false);
Panel p2 =new Panel();
p2.setBackground(Color.BLUE);
p2.add(cb1); p2.add(cb2); p2.add(cb3); p2.add(cb4);
ComponentTestFrame frame = new
ComponentTestFrame("PanelTest");
frame.add(p1); frame.add(p2);
frame.setVisible(true);
}
public static void main(String[] args) {
PanelTest test =new PanelTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
74
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
75
V du ve Panel
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
76
Dialog
L ca sc c thanh tiu de, ducng bin
Thucng dng de cho php nhp du lieu
Nt Dialog phai tcn tai trong mt Frame hoc Dialog khc
Nt Dialog c the l modal hoc non-modal
Modal dialog
Non-modal dialog
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
77
Dialog
Nt sc phucng thc
Dialog(Dialog)
Dialog(Dialog, String)
Dialog(Dialog, String, boolean)
Dialog(Frame)
Dialog(Frame, String)
Dialog(Frame, String, boolean)
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
78
V du Dialog
import java.awt.*;
import java.awt.event.*;
public class DialogTest implements WindowListener {
public DialogTest() {
super();
ComponentTestFrame frame = new
ComponentTestFrame("DialogTest");
Dialog d1 =new Dialog(frame,"Modal Dialog Test",true);
d1.add(new Label("Modal Dialog Test"));
d1.addWindowListener(this);
Dialog d2 =new Dialog(frame,"NonModal Dialog
Test",false);
d2.addWindowListener(this);
d2.add(new Label("NonModal Dialog Test"));
frame.setLocation(100,100);
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
79
V du Dialog
frame.setVisible(true);
d2.pack();
d2.setLocation(250,280);
d2.setVisible(true);
d1.pack();
d1.setLocation(220,170);
d1.setVisible(true);
}
public void windowOpened(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
((Dialog)arg0.getSource()).setVisible(false);
}
public void windowClosed(WindowEvent arg0) {
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
80
V du Dialog
public void windowIconified(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowActivated(WindowEvent arg0) {
}
public void windowDeactivated(WindowEvent arg0) {
}
public static void main(String[] args) {
DialogTest test =new DialogTest();
}
}
C
C
c
c
t
t
h
h
n
n
h
h
p
p
h
h
n
n
A
A
W
W
T
T
81
V du Dialog
82
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
Layout manager: quan l cch trnh by ca cc GUI
components trong mt Container
Cc layout manager
FlowLayout
BorderLayout
CardLayout
GridLayout
GridBagLayout
S dung phucng thc setLayout() ca mt Container de
thay dci layout
83
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
FlowLayout
L layout mc djnh cho Applet v Panel
Cc thnh phn ducc sp xep t tri sang phai, trn xucng duci
Cc hm khci tao
FlowLayout()
FlowLayout(int alignment)
Alignment c the l FlowLayout.CENTER, LEFT, RI GHT
FlowLayout(int, int, int)
84
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
FlowLayout
import java.awt.*;
import java.awt.event.*;
public class FlowLayoutTest extends Frame implements WindowListener {
public FlowLayoutTest(String title){
super(title);
addWindowListener(this);
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 50));
for(int i =1; i<15; i++)
add(new Button("Button"+i));
setSize(575, 300);
setVisible(true);
}
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
85
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
FlowLayout
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public static void main(String[] args) {
FlowLayoutTest ft =new FlowLayoutTest("FlowLayout Test");
}
}
86
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
FlowLayout
87
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
BorderLayout
L layout mc djnh cho Frame, Window, Dialog
Cc thnh phn ducc sp xep trn 5 khu vuc khc nhau
NORTH Dt c dinh ca container.
EAST Dt pha bn phai ca container.x`
SOUTH Dt c pha duci ca container.
WEST Dt pha bn tri ca container.
CENTER Dt c giua ca container.
De thm mt thnh phn vo vng North
Button b1=new Button(North Button); // khai bo thnh phn
setLayout(new BorderLayout()); // thiet lp layout
add(b1,BorderLayout.NORTH); // thm thnh phn vo layout
Khci tao: BorderLayout(), BorderLayout(int, int)
88
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
BorderLayout
89
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
CardLayout
Tu tm hieu trong API Documentation
90
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
GridLayout
Chia Container thnh cc luci bng nhau
Cc Component ducc dt trong cc
Nci luci nn cha t nht mt thnh phn
V du
GridLayout l1 = new GridLayout(4, 3)
Neu new GridLayout(0,3) nghia l c 3 ct, sc hng l bt ky
Khng ducc s dung new GridLayout(0,0);
Cc hm khci tao:
GridLayout(int), GridLayout(int, int), GridLayout(int, int, int
,int)
91
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
GridLayout
import java.awt.*;
import java.awt.event.*;
public class GridLayoutTest extends Frame implements WindowListener {
public GridLayoutTest(String title){
super(title);
addWindowListener(this);
setLayout(new GridLayout(0, 3, 5, 10));
for(int i =1; i<15; i++)
add(new Button("Button"+i));
pack();
setVisible(true);
}
92
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
GridLayout
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public static void main(String[] args) {
GridLayoutTest ft =new GridLayoutTest(GridLayout Test");
}
}
93
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
GridLayout
94
Q
Q
u
u
n
nl
l
t
t
r
r
n
n
h
h
b
b
y
y
GridBagLayout
Tu doc
95
WindowEvent
Ci dt giao tiep WindowListener
Xem v du ve Frame
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
96
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
WindowEvent
Ci dt giao tiep WindowListener
Xem v du ve Frame
Adapter class
WindowAdapter
FocusAdapter
KeyAdapter
MouseAdapter
MouseMotionAdapter
97
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
ActionEvent
Ducc pht sinh bci Button, MenuI tem, TextField, List
Lcp nghe ci dt giao tiep ActionListener hay ci dt phucng thc
actionPerformed(ActionEvent)
Nt sc bien & phucng thc ca ActionEvent
int ALT_MASK: phm ALT c ducc nhn ?
int CTRL_MASK: phm CTRL c ducc nhn ?
int SHI FT_MASK: phm SHI FT c ducc nhn ?
int getModifiers(): c the tra ve ALT_MASK, CTRL_MASK
String getActionCommand(): tra ve command gn vci mci
ActionEvent
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
ActionEvent
import java.awt.*;
import java.awt.event.*;
class ActionListenerTest extends GUIFrame implements ActionListener {
Panel controlPanel, whoDoneItPanel, commandPanel;
MenuBar menuBar;
Menu menu;
MenuItem menuItem;
Button button;
List list;
Label whoDoneItLabel, commandLabel;
TextField whoDoneItTextField, commandTextField, textField;
public ActionListenerTest(){
super("ActionListener Test");
//create menu bar
menuBar =new MenuBar();
menu =new Menu("A Menu");
98
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
ActionEvent
Label.RIGHT);
menuItem =new MenuItem("A Menu Item",new
MenuShortcut(KeyEvent.VK_M));
menuItem.addActionListener(this);
menu.add(menuItem);
menuBar.add(menu);
setMenuBar(menuBar);
//create whoDoneItPanel
whoDoneItPanel =new Panel();
whoDoneItPanel.setBackground(Color.pink);
whoDoneItLabel =new Label("Who done it",
whoDoneItTextField =new TextField("A TextField");
//whoDoneItTextField.addActionListener(this);
whoDoneItTextField.setEditable(false);
whoDoneItPanel.add(whoDoneItLabel);
whoDoneItPanel.add(whoDoneItTextField);
add(whoDoneItPanel,BorderLayout.NORTH);
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
9
9
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
ActionEvent
//create controlPanel
controlPanel =new Panel();
controlPanel.add(new Label("A TextField", Label.RIGHT));
textField =new TextField(15);
textField.addActionListener(this);
controlPanel.add(textField);
button =new Button("A Button");
button.addActionListener(this);
button.setActionCommand("My Action Commmand");
controlPanel.add(button);
controlPanel.add(new Label("A List",Label.RIGHT));
list =new List(5,false);
list.add("Breakfast");
list.add("Lunch");
list.add("Diner");
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
ck"); list.add("Dessert");
100
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
101
ActionEvent
list.add("Brunch");
list.addActionListener(this);
controlPanel.add(list);
add(controlPanel, BorderLayout.CENTER);
//create commandPanel
commandPanel =new Panel();
commandLabel =new Label("Action Command");
commandPanel.setBackground(Color.pink);
commandTextField =new TextField(15);
commandTextField.setEditable(false);
commandPanel.add(commandLabel);
commandPanel.add(commandTextField);
add(commandPanel,BorderLayout.SOUTH);
pack();
setVisible(true);
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
102
}
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
103
ActionEvent
public void actionPerformed(ActionEvent e){
if(e.getSource()==menuItem){
whoDoneItTextField.setText("A MenuItem");
}else if(e.getSource()==textField){
whoDoneItTextField.setText("A TextField");
}else if(e.getSource()==button){
whoDoneItTextField.setText("A Button");
}else if(e.getSource()==list){
whoDoneItTextField.setText("A List");
}
commandTextField.setText(e.getActionCommand());
}
public static void main(String[] args){
ActionListenerTest test =new ActionListenerTest();
}
}
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
104
ActionEvent
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
105
I temEvent
Ducc tao ra t cc thnh phn cho php lua chon nhu Checkbox,
Choice, List
Lcp nghe I temEvent cn ci dt giao tiep I temListener
Phucng thc cn ci dt: itemStateChanged(I temEvent)
Phucng thc ca I temEvent
int getStateChange(): c the nhn I temEvent.SELECTED hoc
I temEvent.DESELECTED
Object getI tem(): item d thay dci trang thi (Checkbox,
Choice hoc item ducc chon ca List)
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
106
TextEvent
Ducc tao ra bci TextComponent (TextField, TextArea)
Lcp nghe ci dt giao tiep TextListener
Phucng thc cn ci dt textValueChanged(TextEvent)
TextEvent ducc sinh ra khi gi trj text ca TextComponent thay
dci (thm, xa text)
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
107
MouseEvent
Ducc tao ra bci chut ca nguci dng
Lcp nghe ci dt giao tiep
MouseListener
MouseMotionListener
Phucng thc ca
MouseEvent
int getClickCount()
Point getPoint()
int getX()
int getY()
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
108
MouseEvent
Cc phucng thc ca
MouseListener
void mouseClicked(MouseEvent)
void mouseEnteredMouseEvent)
void mouseExited(MouseEvent)
void mousePressed(MouseEvent)
void mouseReleased(MouseEvent)
Cc phucng thc ca MouseMotionListener
void mouseMoved(MouseEvent)
void mouseDragged(MouseEvent)
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
MouseEvent
import java.awt.*;
import java.awt.event.*;
public class MouseTest extends
GUIFrame
implements MouseListener, MouseMotionListener {
Canvas canvas;
Label location, event;
public MouseTest(){
super("Mouse Event Test");
canvas =new Canvas();
canvas.setBackground(Color.white);
canvas.setSize(450,450);
canvas.addMouseListener(this);
canvas.addMouseMotionListener(this);
add(canvas, BorderLayout.CENTER);
Panel infoPanel =new Panel();
infoPanel.setLayout(new GridLayout(0, 2, 10, 0));
location =new Label("Location: ");
108
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
MouseEvent
infoPanel.add(location);
event =new Label("Event: ");
infoPanel.add(event);
add(infoPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MouseTest();
}
public void mouseClicked(MouseEvent e){
String text ="Event: Clicked Button ";
switch(e.getModifiers()){
case InputEvent.BUTTON1_MASK:
text +=1;
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
case InputEvent.BUTTON2_MASK:
109
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
MouseEvent
text +=2;
break;
case InputEvent.BUTTON3_MASK:
text +=3;
break;
}
text +=" (" +e.getClickCount() +"x)";
event.setText(text);
}
public void mouseEntered(MouseEvent e){
event.setText("Event: Entered");
}
public void mouseExited(MouseEvent e){
event.setText("Event: Exited");
}
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
110
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
MouseEvent
public void mousePressed(MouseEvent e){
event.setText("Event: Pressed");
}
public void mouseReleased(MouseEvent e){
event.setText("Event: Released");
}
public void mouseMoved(MouseEvent e){
location.setText("Location: (" +e.getX()+"," +
e.getY()+")");
}
public void mouseDragged(MouseEvent e){
Point p =e.getPoint();
event.setText("Event: Dragged");
location.setText("Location: (" +p.getX()+"," +
p.getY()+")");
}
}
111
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
112
MouseEvent
X
X
l
l
c
c
c
c
s
s
k
k
i
i
n
n
113
KeyEvent
Ducc tao ra khi nguci dng g bn phm
Lcp nghe ci dt giao tiep KeyListener
Cc phucng thc ca KeyListener
void keyPressed(KeyEvent)
void keyReleased(KeyEvent)
void
keyTyped(KeyEvent)
Nt sc phucng thc
ca KeyEvent
char getKeyChar()
int getKeyCode()
String
getKeyText(int
keyCode)
114
V
V
n
n
h
h
Nap anh
I mage image = Toolkit.getDefaultToolkit().
getI mage(fileName);
import java.awt.*;
public class ImageTest extends Canvas {
public ImageTest(){
super();
setSize(300,200);
setBackground(Color.white);
}
public void paint(Graphics g){
Image image =
Toolkit.getDefaultToolkit().getImage("image.jpg");
115
V
V
n
n
h
h
Nap anh
g.drawImage(image, 50,50, this);
}
116
V
V
n
n
h
h
Nap anh
public static void main(String[] args) {
ImageTest image =new ImageTest();
GUIFrame frame =new GUIFrame("ImageTest");
frame.add(image);
frame.pack();
frame.setVisible(true);
}
}
117
V
V
n
n
h
h
Nap anh
C
C
U
U
H
H
D
D
I
I
118
B
B
i
i
t
t
p
p
Tao chucng trnh (Frame) c cha mt Canvas, kch
thucc 200, 200 mu dc. Khi dua chut vo Canvas
chucng trnh hien dng chu I n canvas, khi dua chut ra
ngoi se hien dng chu Not in canvas
Viet chucng trnh cho php ve ducng trn tai mt vj tr
bt ky m nguci dng nhn chut trn mn hnh
Viet mt chucng trnh vci menu Draw c 3 menu item:
line, circle, square: khi nguci dng kch vo tng menu
item, 50 line hoc circle hoc square se ducc ve ra
119
B
B
i
i
t
t
p
p
Viet mt chucng trnh tnh ton dcn gian
120
B
B
i
i
t
t
p
p
Viet chucng trnh cho php nguci dng dieu khien mt
qua bng. Trn mn hnh c cc nt l: To, Nhc, Tri,
Phai, Ln, Xucng. Khi nguci dng n 1 nt th kch cc/vj
tr ca qua bng se thay dci theo. Yu cu tao mt lcp
Ball ring biet. (Mc rng bi ton cho trucng hcp nguci
dng nhn chut truc tiep trn mn hnh)
Viet chucng trnh m ta tr chci d mn. Trn mn hnh
c 3x3 nt bm v mci nt c the l c mn hoc khng
(ngu nhin). Khi nguci dng nhn mt nt, neu nt d
khng c mn th cho php nguci dng n tiep, cn khng
th thng bo mn nc v dng lai. Luu l mci nt c
mt sc v nguci dng c the nhn phm sc tucng ng
thay v nhn chut vo nt.
121
Su kien v dci tucng gy ra su kien
T
T
h
h
a
a
m
m
k
k
h
h
o
o
122
T
T
h
h
a
a
m
m
k
k
h
h
o
o
Su kien v dci tucng gy ra su kien
123
T
T
h
h
a
a
m
m
k
k
h
h
o
o
Dci tucng nghe v phucng thc cn ci dt
124
T
T
h
h
a
a
m
m
k
k
h
h
o
o
Dci tucng nghe v phucng thc cn ci dt
125
T
T
h
h
a
a
m
m
k
k
h
h
o
o
Listener v cc thnh phn tucng ng
WindowListener
ItemListener
Dialog
Frame
ActionListener
Button
Choice
Checkbox
List
List
MenuItem
TextField
126
T
T
h
h
a
a
m
m
k
k
h
h
o
o
Listener cho Component
Component
ComponentListener
FocusListener
KeyListener
MouseListener
MouseMotionLIstener