10 awt event model

14
10 AWT Event Model HUREE University ICT Instructor: M.J LEE

description

 

Transcript of 10 awt event model

Page 1: 10 awt event model

10 AWT Event ModelHUREE University ICT

Instructor: M.J LEE

Page 2: 10 awt event model

2

Event◦Actions like clicking button with a mouse,

pushing down a key on keyboard Event Source

◦Sources that cause events such as a button, a key

Event Handler◦Methods that get an occurred event, then

handle proper actions

Contents

Page 3: 10 awt event model

3

When something happen(event occurs), this method handle the event.

How to handle event?

import java.awt.*;public class ButtonTest {

public static void main(String args[]) {Frame f = new Frame("Button Test");Button b = new Button(“Click!");b.addActionListener(new

ButtonHandler());f.add(b);f.pack();f.setVisible(true);

}}

Page 4: 10 awt event model

4

When something happens(a event occurs), this method handle the event.

How to handle event?

import java.awt.event.*;public class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {System.out.println(“button pushed!");System.out.println(e.getActionCommand());

}}

Page 5: 10 awt event model

5

Method Categories and InterfacesCategory Interface Name Methods

Action ActionListener actionPerformed(ActionEvent)

Item ItemListener itemStateChanged(ItemEvent)

Mouse motion MouseMotionListener mouseDragged(MouseEvent)

    mouseMoved(MouseEvent)

Mouse button MouseListener mousePressed(MouseEvent)

    mouseReleased(MouseEvent)

    mouseEntered(MouseEvent)

    mouseExited(MouseEvent)

    mouseClicked(MouseEvent)

Key KeyListener keyPressed(KeyEvent)

    keyReleased(KeyEvent)

    keyTyped(KeyEvent)

Focus FocusListener focusGained(FocusEvent)

    focusLost(FocusEvent)

Page 6: 10 awt event model

6

Category Interface Name Methods

Adjustment AdjustmentListner adjustmentValueChanged(AdjustmentEvent)

Component ComponentListener componentMoved(ComponentEvent)

    componentHidden(ComponentEvent)

    componentResized(ComponentEvent)

    componentShown(ComponentEvent)

Window WindowListener windowClosing(WindowEvent)

    windowOpened(WindowEvent)

    windowIconified(WindowEvent)

    windowDeiconified(WindowEvent)

    windowClosed(WindowEvent)

    windowActivated(WindowEvent)

    windowDeactivated(WindowEvent)

Container ContainerListener componentAdded(ContainerEvent)

    componentRemoved(ContainerEvent)

Text TextListener textValueChanged(TextEvent)

Page 7: 10 awt event model

7

We want to close the window when pushing down a close button

How to implement? Which method do we have to use?

1. Find out event source2. Apply addXXXListener() on the event

source, define event handler3. Implement event handler

Examples

Page 8: 10 awt event model

8

1. Find out event source windowWhat is the interface related with window? WindowListener

2. Match the event source with event handler

3. Implement WindowHandler()

Examples

f.addWindowListener(new WindowHandler());

Page 9: 10 awt event model

9

Examplespublic class WindowHandler implements WindowListener{

public void windowClosing(WindowEvent e) {System.exit(0);

}windowOpened(WindowEvent e) { }windowIconified(WindowEvent e) { }windowDeiconified(WindowEvent e) { }windowClosed(WindowEvent e) { }windowActivated(WindowEvent e) { }windowDeactivated(WindowEvent e) { }

}

Page 10: 10 awt event model

10

public class ButtonTest2 implements WindowListener {private Frame f;private Button b;public ButtonTest2(String str) {

f = new Frame(str);f.addWindowListener(this);b = new Button("Click!!");b.addActionListener(new ButtonHandler());f.add(b, "Center");f.pack();f.setVisible(true);

}public static void main(String args[]) {

ButtonTest2 fa= new ButtonTest2("Button Test");}public void windowClosing(WindowEvent e) {

f.setVisible(false); f.dispose(); System.exit(0);

}public void windowOpened(WindowEvent e) { }public void windowIconified(WindowEvent e) { }public void windowDeiconified(WindowEvent e) { }public void windowClosed(WindowEvent e) { }public void windowActivated(WindowEvent e) { }public void windowDeactivated(WindowEvent e) { }

}

Page 11: 10 awt event model

11

public class ButtonTest extends Frame implements WindowListener {public ButtonTest(String str) {

super(str);addWindowListener(this);

}public static void main(String args[]) {

ButtonTest bt= new ButtonTest("Button Test");Button b = new Button("Click!!");b.addActionListener(new ButtonHandler());bt.add(b, "Center");bt.pack();bt.setVisible(true);

}public void windowClosing(WindowEvent e) {

setVisible(false); dispose(); System.exit(0);

}public void windowOpened(WindowEvent e) { }public void windowIconified(WindowEvent e) { }public void windowDeiconified(WindowEvent e) { }public void windowClosed(WindowEvent e) { }public void windowActivated(WindowEvent e) { }public void windowDeactivated(WindowEvent e) { }

}

import java.awt.event.*;public class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {System.out.println("button pushed!");System.out.println(e.getActionCommand());

}}

Page 12: 10 awt event model

12

Multiple Listener We can add multiple event handlers on the

one event source Sensing moving, and being clocked. MouseMotionListener / MouseListener

interface

1. Define a event source2. Add each event handler on the event

source3. Implement two Event Handler

Page 13: 10 awt event model

public class TwoListen implements MouseMotionListener, MouseListener {

private Frame f; private TextField tf;

public static void main(String args[]) { TwoListen two = new TwoListen(); two.go();

} public void go() {

f = new Frame("Two listeners example"); f.add (new Label ("Click and drag the mouse"), BorderLayout.NORTH); tf = new TextField (30);f.add (tf, BorderLayout.SOUTH);f.addMouseMotionListener(this);f.addMouseListener (this);f.setSize(300, 200);f.setVisible(true);

}... ...

1 Event source

2 Add listeners

Page 14: 10 awt event model

...

...

public void mouseDragged (MouseEvent e) { String s = "Mouse dragging: X=“+e.getX()+" Y=“+ e.getY();

tf.setText (s); } public void mouseEntered (MouseEvent e) {

String s = "The mouse entered"; tf.setText (s);

} public void mouseExited (MouseEvent e) {

String s = "The mouse has left the building"; tf.setText (s);

}

public void mouseMoved (MouseEvent e) { }public void mousePressed (MouseEvent e) { }public void mouseClicked (MouseEvent e) { }public void mouseReleased (MouseEvent e) { }

}

Implement all abstract methods in Listeners

3