Tải bản đầy đủ (.docx) (114 trang)

lập trình đồ họa với awt tiếng việt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.51 MB, 114 trang )

L

p

trình h

a

v

i

AWT
CAO

Duc

Thong



Thanglong

University

N

i

dun
g


ng quan AWT
Các thành ph

n AWT
Qu

n lý trình bày
lý ki

n
2
Th

vi

nAWT
AWT là vi

t t a Abstract Windowing Toolkit
AWT cho phép o các thành ph

n
a
AWT cho phép nh

n li

u chu

t, bàn phím

Các thành ph

n a n a AWT
t ch

a (Container)
Thành ph

n (Component)
Trình qu

n lý trình bày (Layout Manager)
a (Graphics), phông ch

(Font), ki

n
(Event)
3
Th

vi

nAWT
Object Color Event Font
Graphics
Image Insets Polygon
Rectangle
Toolkit MenuComponent
MenuBar

XXXLayout
MenuItem
CheckboxGroup
M
enu
Component
C
heckbox
M
enuItem
4
Component
Button
Canvas
Checkbox
Choice
Container
Label
List
Scrollbar
TextComponent
Th

vi

nAWT
AWTException

AWTError
Applet

Panel
Frame
Window
Dialog
ScrollPane
F
ileDialog
TextArea
TextField
5
Cácthànhph

n
AWT
Component (thành ph

n)
Là t i ng a có th

hi

n th c
trên màn hình và
có th ng tác i ng i dùng
Là t abstract superclass cho u t các
component a AWT
Ví Component: button, checkbox, scrollbar…
t ph ng th

c n a Component

getBackGround(): tr u n a Component
getBounds(): tr ph

m vi a Component
(Rectangle)
getFont(): tr font hi

n i a Component
getForeGround(): tr màu a Component
getHeight(): tr chi

u cao a Component (pixel,
ki

u int)
6
Cácthànhph

n
AWT
t ph ng th

c n a Component
getSize(): tr kích th c a Component
(Dimenstion)
getWidth(): tr chi

u ng a Component (int)
getX(), getY(): tr a hi


n i
isEnable(): boolean
paint(Graphics): ch u trách nhi

m hi

n th

component
repaint(): c i i giao di

n cho component
setVisible(boolean): hi

n th

component
7
Cácthànhph

n
AWT
Container (v

t ch

a)
Ch

a trong gói java.awt

Là vùng có th t các thành ph

n giao di

n
t lo

i Container: Panel, Frame, Dialog
Có t Component có kh ng ch

a các Component
khác
thêm t Component vào Container ta
ng ph
ng th

c
add(Component)
Container ng t “layout manager”
p p các
Component
8
Cácthànhph

n
AWT
Frame
Th

a Window nên ng là t Container

Frame a là Component a là Container
o Frame
Frame()
Frame(String title)

9
Cácthànhph

nAWT
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();
}
}
10
Cácthànhph

n
AWT
Frame
11
X


l
ý

cács

ki

n
k
i

n là gì
Khi ng i dùng th

c hi

n t hành ng trên
GUI, t ki

n
c sinh ra.
Các ki

n là các i ng mô nh

ng gì

ã
y ra
i tác ng a ng i dùng o ra nh


ng lo

i ki

n khác
nhau
Kích chu

t
Button
ActionEvent

Eve
ntHandler
actionPerformed(ActionEvent e)
{
//Do something
}
12
Event Sources
X



cács

ki

n

Là ngu

n sinh ra
ki

n.
ki

n c sinh ra i ng t object, ví
ActionEvent.
Event Handlers
Event handler là t ph ng th

c, nó nh

n
t i
ng
event, gi

i mã và lý các ng tác i ng
i dùng
p ch

a các ph ng th

c (event handler)
c
i là
p nghe

ki

n (listener)
t p nghe có th lý t ki

n nào

ó, ngu

n sinh ra
ki

n n ph

i ng ký p nghe
t ngu

n sinh ra ki

n có th

có nhi

u
p nghe
t p mu

n là listener ph

i implement t

giao ti

p thích p
13
X

l
ý

cács

ki

n
V
í

ki

n
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);
}
1
4
X

l
ý

cács

ki

n
V
í

ki

n
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);
}
}
15
X

l
ý

các
s

k
i

n
V
í

ki

n

16
X

l
ý


cs

ki

n
C
ác lo

i
ki

n
(Even
t
java.util.EventObject
java.awt.AWTEvent
ActionEvent
java.awt.event
AdjustmentEv
ent
ComponentEve
nt
ItemEvent

FocusEvent
InputEvent
ContainerEvent
KeyEvent
MouseEvent
TextEvent WindowEvent
17
X

l
ý

các
s

ki

n
M
ô
ki

n
18
Các lo

i Listener
X




cács

ki

n
ActionListener
AdjustmentListener
E
v

ContainerListener
e
n
FocusListener
L
i
ItemListener
s
t
KeyListener
e
n
MouseListener
e
r
MouseMotionListener
TextListener
WindowListener
19

ng ký i ng nghe
add + lo

i ki

n + Listener(
ki

n)
Ví i nút Button
addActionListener(ActionListener)
X



cács

ki

n


i danh s
ách
List
addActionListener(
A
ctionListener)
addItemListener(It
e

mListener)
20
X

l
ý


cs

ki

n
C
ài
t qu

n lý
ki

n
Xác nh i ng gây ra ki

n (source)
Xác nh ki

n có th y ra ng

ng
i i ng mà ta n

qu

n lý (object)
Xác nh i ng “nghe” (listener) và cài
t các ph ng th

c
ng

ng
ng ký i ng nghe cho i ng gây ra
ki

n
21
X



cács

ki

n

qu

n lý
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");
}
public void init(){
b.addActionListener(new ButtonHandler());
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
22
X

l
ý

cács

ki

n
V

í
qu

n lý
ki

n
public static void main(String[] args){
TestButton test = new TestButton();
test.init();
}
}
X



cács

ki

n
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Button's command is: "+
e.getActionCommand());
}
}
23
X


l
ý

các
s

k
i

n
V
í
qu

n

ki

n
Button's command is: ButtonPressed
Button's command is: ButtonPressed
Button's command is: ButtonPressed
24
Cácthànhph

nAWT
Frame dùng test các thành ph

n khác
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);
}
25
Cácthànhph

nAWT
Frame dùng test các thành ph

n khác
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){}

}
26
Cácthànhph

nAWT
t ph ng th

c a Frame
Frame()
Frame(String)
Image getIconImage()
MenuBar getMenuBar()
String getTitle()
Boolean isResizeable()
setIconImage(Image)
setMenuBar(MenuBar)
setTitle(String)
setVisible(boolean)
27
Cácthànhph

nAWT
GUIFrame

×