10 awt event model
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

10 awt event model

on

  • 2,081 views

 

Statistics

Views

Total Views
2,081
Views on SlideShare
2,081
Embed Views
0

Actions

Likes
1
Downloads
101
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

10 awt event model Presentation Transcript

  • 1. 10 AWT Event Model
    HUREE University ICT
    Instructor: M.J LEE
  • 2. Contents
    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
    2
  • 3. How to handle event?
    When something happen(event occurs), this method handle the event.
    3
    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);
    }
    }
  • 4. How to handle event?
    When something happens(a event occurs), this method handle the event.
    4
    import java.awt.event.*;
    public class ButtonHandlerimplements ActionListener{
    public void actionPerformed(ActionEvent e) {
    System.out.println(“button pushed!");
    System.out.println(e.getActionCommand());
    }
    }
  • 5. Method Categories and Interfaces
    5
  • 6. 6
  • 7. Examples
    We want to close the window when pushing down a close button
    How to implement? Which method do we have to use?
    Find out event source
    Apply addXXXListener() on the event source, define event handler
    Implement event handler
    7
  • 8. Examples
    Find out event source  window
    What is the interface related with window? WindowListener
    Match the event source with event handler
    Implement WindowHandler()
    8
    f.addWindowListener(new WindowHandler());
  • 9. Examples
    9
    public class WindowHandlerimplementsWindowListener{
    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) { }
    }
  • 10. 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) { }
    }
  • 11. 11
    public class ButtonTest extends Frame implements WindowListener {
    public ButtonTest(String str) {
    super(str);
    addWindowListener(this);
    }
    public static void main(String args[]) {
    ButtonTestbt= 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());
    }
    }
  • 12. Multiple Listener
    12
    • We can add multiple event handlers on the one event source
    • 13. Sensing moving, and being clocked.
    • 14. MouseMotionListener / MouseListener interface
    Define a event source
    Add each event handler on the event source
    Implement two Event Handler
  • 15. public class TwoListen implements MouseMotionListener, MouseListener {
    private Frame f;
    private TextFieldtf;
    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
    Add listeners
    2
  • 16. ...
    ...
    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