LẬP TRÌNH ĐỒ HỌA VỚI AWT - Pdf 32



L
L


p
p

t
t
r
r

T
T
CAO

Duc

Thong



Thanglong

University


2

N
N


i
i
d

AWT

Qun



trình

bày







kin

3
T
T
h
h



a

Abstract

Windowing

Toolkit

AWT

cho

phép

o

các

thành

phn



a


phn



a



n

a

AWT

t

cha

(Container)

Thành

phn


ch

(Font),



kin

(Event)

4
T
T
h
h


v
v
i
i


n
n
A
A
W
W
T


Toolk
i
t

MenuComponent
XXXLayout

CheckboxGroup

Component

MenuBar

MenuItem



n
n
A
A
W
W
T
T
Component
Button
CanvasTextComponent
Panel
Window
Scroll
Pane TextArea

TextField

á
á
c
c
t
t
h
h
à
à
n
n
h
h
p
p
h
h


n
n
A
A
W
W
T
T

hin

th

c

trên

màn

hình





th

ng

tác

i

ngi

dùng



Component:

button,

checkbox,

scrollbar…

t



phng

thc



n

a

Component


a

Component

(Rectangle)

getFont():

tr



font

hin

i

a

Component

getForeGround():



kiu

int)

7
C
C
á
á
c
c
t
t
h
h
à
à
n
n
h
h
p
p
h
h


n
n




getSize():

tr



kích

thc

a

Component

(Dimenstion)

getWidth():

tr



chiu


isEnable():

boolean

paint(Graphics):

chu

trách

nhim

hin

th

component

repaint():

c

i


c
c
t
t
h
h
à
à
n
n
h
h
p
p
h
h


n
n
A
A
W
W
T
T


th

t

các

thành

phn

giao

din

t



loi

Container:

Panel,

Frame,

Dialog


t

Component

vào

Container

ta



ng

phng

thc
add(Component)

Container



ng

t

h
h
p
p
h
h


n
n
A
A
W
W
T
T


Frame

Tha



Container

o

Frame

Frame()

Frame(String

title)






T



Frame

import java.awt.*;
public class UseLessFrame
extends
Frame {
public
UseLessFrame(){
super("Useless
Frame");
setS
ize(300,200);
setVisible(true);
}
public static
void


h
h


n
n
A
A
W
W
T
T


Frame
12
X
X






kin



gì

Khi

ngi

dùng

thc

hin

t

hành

ng



i

ng





nhng



ã

y

ra

i

tác

ng

aButtonActionEvent
EventHandler

actionP
erformed(ActionEvent
e){

//Do
something
}
13
X
X


l
l
ý
ý
c
c





ngun

sinh

ra



kin.



kin

c

sinh

ra

i

ng


thc,



nhn



t

i

ng
event,

gii









các

ng



p

nghe



kin

(listener)



t

p

nghe



th






nghe

t

ngun

sinh

ra



kin



th



nhiu

p

nghe

l
l
ý
ý
c
c
á
á
c
c
s
s


k
k
i
i


n
n


TextField t
f1
=
new
TextField(5);
TextField t
f2
=
new
TextField(5);
Button btnResult =
new

Button("Dou
ble i
s");
Button ext = ne
w
Button("exit");
public EventTest(String title) {
super(title);
setLayout(new
FlowLayout());
btnResult.addActionListener(this);
ext.addActionListener(this);
add(lab);
add(tf1);

add(btnR
esult);


n
n 













kin
public

new
EventTest("Event
handling");

t.setS
ize(300,

200);

t.setVisible(true);

}
}

16
X
X


l
l
ý
ý
c
c
á
á
c
c
s



kin 17
X
X


l
l
ý
ý
c
c
á
á
c
c
s
s


k
k
i

java.awt.AWTEvent

ActionEvent

java.awt.event AdjustmentEvent ComponentEvent
ItemEvent

FocusEvent
InputEvent
ContainerEvent

KeyEvent

k
k
i
i


n
n








kin19
X


Các

loi

Listener

ActionListener
AdjustmentListener
E
v
ContainerListener
e
n
FocusListener

t

L

i

c
c
á
á
c
c
s
s


k
k
i
i


n
n



ng








i

nút

Button
addActionListener(ActionListener)







i

danh

sách

List
addActionListener(A
ctionListener)

addItemListener(Ite
mListener)



Cài

t

qun





kin



Xác

nh

i

ng



ng

ng

i

i

ng



ta

n
qun



(object)

Xác

nh

i


ng

nghe

cho

i

ng

gây

ra



kin

X
X


l
l
ý
ý
c
c
á

qun





kin
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");

}

k
k
i
i


n
n
}
22
X
X


l
l
ý
ý
c
c
á
á
c
c
s
s




kin
public static
void

main(String[]

args){

TestButton test =
new
TestButton();
test.init
(
);

}
}

class ButtonHandler implements ActionListener{
public
void
actionPerformed(ActionEvent e) {


n
n

24









qun





kin


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status