eventos en java

22
Module 11

Upload: renhito-kun-roses

Post on 23-Dec-2015

222 views

Category:

Documents


0 download

DESCRIPTION

Teoria basica sobre manipulación de eventos en java.

TRANSCRIPT

Page 1: Eventos en java

� ����� ������� �� �����

Mo

du

le 1

1

Page 2: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �� �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•D

efin

e ev

ents

an

d e

ven

t h

and

lin

g

•W

rite

co

de

to h

and

le e

ven

ts t

hat

occ

ur

in a

GU

I

•D

escr

ibe

the

con

cep

to

fad

apte

rcl

asse

s,in

clu

din

gh

ow

and

wh

en t

o u

se t

hem

•D

eter

min

e th

e u

ser

acti

on

th

at o

rig

inat

ed t

he

even

tfr

om

th

e ev

ent

ob

ject

det

ails

•Id

enti

fy t

he

app

rop

riat

e li

sten

er i

nte

rfac

e fo

r a

var

iety

of

even

t ty

pes

•C

reat

e th

e ap

pro

pri

ate

even

t h

and

ler

met

ho

ds

for

av

arie

ty o

f ev

ent

typ

es

•U

nd

erst

and

th

e u

se o

f in

ner

cla

sses

an

d a

no

ny

mo

us

clas

ses

in e

ven

t h

and

lin

g

Page 3: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �7 �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•W

hat

par

ts o

f a

GU

I ar

e re

qu

ired

to

mak

e it

use

ful?

•H

ow

do

esa

gra

ph

ical

pro

gra

mh

and

lea

mo

use

clic

ko

ran

y o

ther

ty

pe

of

use

r in

tera

ctio

n?

Page 4: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �8 �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•E

ven

ts –

Ob

ject

s th

at d

escr

ibe

wh

at h

app

ened

•E

ven

t so

urc

es –

Th

e g

ener

ato

r o

f an

ev

ent

•E

ven

t h

and

lers

– A

met

ho

d t

hat

rec

eiv

es a

n e

ven

to

bje

ct,d

ecip

her

sit

,an

dp

roce

sses

the

use

r’s

inte

ract

ion

actionPerformed(ActionEvent e) {

...

}

ActionEvent

Page 5: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �9 �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•A

n e

ven

t ca

n b

e se

nt

to m

any

ev

ent

han

dle

rs.

•E

ven

t h

and

lers

reg

iste

r w

ith

co

mp

on

ents

wh

en t

hey

are

inte

rest

ed i

n e

ven

ts g

ener

ated

by

th

at c

om

po

nen

t.

actionPerformed(ActionEvent e) {

...

}actionPerformed(ActionEvent e) {

...

}ActionEvent

Page 6: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �: �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•C

lien

t o

bje

cts

(han

dle

rs)

reg

iste

r w

ith

a G

UI

com

po

nen

t th

at t

hey

wan

t to

ob

serv

e.

•G

UI

com

po

nen

tso

nly

trig

ger

the

han

dle

rsfo

rth

ety

pe

of

even

t th

at h

as o

ccu

rred

.

•M

ost

co

mp

on

ents

can

tri

gg

er m

ore

th

an o

ne

typ

e o

fev

ent.

•T

he

del

egat

ion

mo

del

dis

trib

ute

s th

e w

ork

am

on

gm

ult

iple

cla

sses

.

Page 7: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �; �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1import java.awt.*;

2 3public class TestButton {

4 private Frame f;

5 private Button b;

6 7 public TestButton() {

8 f = new Frame("Test");

9 b = new Button("Press Me!");

10

b.setActionCommand("ButtonPressed");

11

}

1213

public void launchFrame() {

14

b.addActionListener(new ButtonHandler());

15

f.add(b,BorderLayout.CENTER);

16

f.pack();

17

f.setVisible(true);

18

}

XxReNoxX
Typewriter
Page 8: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �< �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1920

public static void main(String args[]) {

21

TestButton guiApp = new TestButton();

22

guiApp.launchFrame();

23

}

24

}

Co

de

for

the

even

t li

sten

er l

oo

ks

lik

e th

is:

1import java.awt.event.*;

2 3publicclass ButtonHandler implements ActionListener {

4 public voidactionPerformed(ActionEvent e) {

5 System.out.println("Action occurred");

6 System.out.println("Button’s command is: "

7 + e.getActionCommand());

8 }

9}

Page 9: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� �= �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

java.util.EventObject

java.awt.AWTEvent

ActionEvent

AdjustmentEvent

ComponentEvent

TextEvent

ContainerEvent

FocusEvent

InputEvent

WindowEvent

KeyEvent

MouseEvent

java.awt.event

ItemEvent

Page 10: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��> �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

Cate

go

ryIn

terf

ace

Nam

eM

eth

od

s

Act

ion

ActionListener

actionPerformed(ActionEvent)

Item

ItemListener

itemStateChanged(ItemEvent)

Mo

use

MouseListener

mousePressed(MouseEvent)

mouseReleased(MouseEvent)

mouseEntered(MouseEvent)

mouseExited(MouseEvent)

mouseClicked(MouseEvent)

Mo

use

mo

tio

nMouseMotionListener

mouseDragged(MouseEvent)

mouseMoved(MouseEvent)

Key

KeyListener

keyPressed(KeyEvent)

keyReleased(KeyEvent)

keyTyped(KeyEvent)

Page 11: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��� �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

Cate

go

ryIn

terf

ace

Nam

eM

eth

od

s

Fo

cus

FocusListener

focusGained(FocusEvent)

focusLost(FocusEvent)

Ad

just

men

tAdjustmentListener

adjustmentValueChanged

(AdjustmentEvent)

Co

mp

on

entComponentListener

componentMoved(ComponentEvent)

componentHidden(ComponentEvent)

componentResized(ComponentEvent)

componentShown(ComponentEvent)

Page 12: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��� �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

Cate

go

ryIn

terf

ace

Nam

eM

eth

od

s

Win

do

wWindowListener

windowClosing(WindowEvent)

windowOpened(WindowEvent)

windowIconified(WindowEvent)

windowDeiconified(WindowEvent)

windowClosed(WindowEvent)

windowActivated(WindowEvent)

windowDeactivated(WindowEvent)

Co

nta

iner

ContainerListener

componentAdded(ContainerEvent)

componentRemoved(ContainerEvent)

Tex

tTextListener

textValueChanged(TextEvent)

Page 13: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��7 �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1import java.awt.*;

2import java.awt.event.*;

3 4public class TwoListener

5implements MouseMotionListener, MouseListener {

6 private Frame f;

7 private TextField tf;

8 9 public TwoListener() {

10

f = new Frame("Two listeners example");

11

tf = new TextField(30);

12

}

Page 14: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��8 �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1314

public void launchFrame() {

15

Label label = new Label("Click and drag the mouse");

16

// Add components to the frame

17

f.add(label, BorderLayout.NORTH);

18

f.add(tf, BorderLayout.SOUTH);

19

// Add this object as a listener

20

f.addMouseMotionListener(this);

21

f.addMouseListener(this);

22

// Size the frame and make it visible

23

f.setSize(300, 200);

24

f.setVisible(true);

25

}

Page 15: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��9 �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

2627

// These are MouseMotionListener events

28

public voidmouseDragged(MouseEvent e) {

29

String s = "Mouse dragging: X = " + e.getX()

30

+ " Y = " + e.getY();

31

tf.setText(s);

32

}

3334

public voidmouseEntered(MouseEvent e) {

35

String s = "The mouse entered";

36

tf.setText(s);

37

}

3839

public voidmouseExited(MouseEvent e) {

40

String s = "The mouse has left the building";

41

tf.setText(s);

42

}

Page 16: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��: �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

4344

// Unused MouseMotionListener method.

45

// All methods of a listener must be present in the

46

// class even if they are not used.

47

public void mouseMoved(MouseEvent e) { }

4849

// Unused MouseListener methods.

50

public void mousePressed(MouseEvent e) { }

51

public void mouseClicked(MouseEvent e) { }

52

public void mouseReleased(MouseEvent e) { }

5354

public static void main(String args[]) {

55

TwoListener two = new TwoListener();

56

two.launchFrame();

57

}

58

}

Page 17: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��; �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•M

ult

iple

list

ener

sca

use

un

rela

ted

par

tso

fap

rog

ram

tore

act

to t

he

sam

e ev

ent.

•T

he

han

dle

rso

fal

lre

gis

tere

dli

sten

ers

are

call

edw

hen

the

even

t o

ccu

rs.

Page 18: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��< �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

•T

he

list

ener

clas

ses

that

yo

ud

efin

eca

nex

ten

dad

apte

rcl

asse

s an

d o

ver

rid

e o

nly

th

e m

eth

od

s th

at y

ou

nee

d.

•A

n e

xam

ple

is:

1import java.awt.*;

2import java.awt.event.*;

3 4public class MouseClickHandler extends MouseAdapter {

5 6 // We just need the mouseClick handler, so we use

7 // an adapter to avoid having to write all the

8 // event handler methods

9 10

public void mouseClicked(MouseEvent e) {

11

// Do stuff with the mouse click...

12

}

13

}

Page 19: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��= �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1import java.awt.*;

2import java.awt.event.*;

3public class TestInner {

4 private Frame f;

5 privateTextField tf; // used by inner class

6 7 public TestInner() {

8 f = new Frame("Inner classes example");

9 tf = new TextField(30);

10

}

1112

classMyMouseMotionListener extends MouseMotionAdapter {

13

public void mouseDragged(MouseEvent e) {

14

String s = "Mouse dragging: X = "+ e.getX()

15

+ " Y = " + e.getY();

16

tf.setText(s);

17

}

18

}

Page 20: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��> �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1920

public void launchFrame() {

21

Label label = new Label("Click and drag the mouse");

22

// Add components to the frame

23

f.add(label, BorderLayout.NORTH);

24

f.add(tf, BorderLayout.SOUTH);

25

// Add a listener that uses an Inner class

26

f.addMouseMotionListener(new MyMouseMotionListener());

27

f.addMouseListener(new MouseClickHandler());

28

// Size the frame and make it visible

29

f.setSize(300, 200);

30

f.setVisible(true);

31

}

3233

public static void main(String args[]) {

34

TestInner obj = new TestInner();

35

obj.launchFrame();

36

}

37

}

Page 21: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��� �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

1import java.awt.*;

2import java.awt.event.*;

3 4public class TestAnonymous {

5 private Frame f;

6 private TextField tf;

7 8 public TestAnonymous() {

9 f = new Frame("Anonymous classes example");

10

tf = new TextField(30);

11

}

1213

public static void main(String args[]) {

14

TestAnonymous obj = new TestAnonymous();

15

obj.launchFrame();

16

}

17

Page 22: Eventos en java

� ����� ������� �� �����

� �� �� ��� ����� ��� �� ��

� ����� !"#$$%& '()� *��+�+" ,-+ ./ (* 01223� !" +3 ,+,�4,5 0& '(& ,�4� *,+ .3 ,4� +� �(6

18

public void launchFrame() {

19

Label label = new Label("Click and drag the mouse");

20

// Add components to the frame

21

f.add(label, BorderLayout.NORTH);

22

f.add(tf, BorderLayout.SOUTH);

23

// Add a listener that uses an anonymous class

24

f.addMouseMotionListener(new MouseMotionAdapter() {

25

public void mouseDragged(MouseEvent e) {

26

String s = "Mouse dragging: X = "+ e.getX()

27

+ " Y = " + e.getY();

28

tf.setText(s);

29

}

30

});// <- note the closing parenthesis

31

f.addMouseListener(new MouseClickHandler()); // Not shown

32

// Size the frame and make it visible

33

f.setSize(300, 200);

34

f.setVisible(true);

35

}

36

}