Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

10 awt event model

2,019 views

Published on

Published in: Technology, Education
  • Be the first to comment

10 awt event model

  1. 1. 10 AWT Event Model<br />HUREE University ICT<br />Instructor: M.J LEE<br />
  2. 2. Contents<br />Event<br />Actions like clicking button with a mouse, pushing down a key on keyboard<br />Event Source<br />Sources that cause events such as a button, a key<br />Event Handler<br />Methods that get an occurred event, then handle proper actions<br />2<br />
  3. 3. How to handle event?<br />When something happen(event occurs), this method handle the event.<br />3<br />import java.awt.*;<br />public class ButtonTest {<br /> public static void main(String args[]) {<br /> Frame f = new Frame("Button Test");<br /> Button b = new Button(“Click!");<br />b.addActionListener(new ButtonHandler());<br />f.add(b);<br />f.pack();<br />f.setVisible(true);<br /> }<br />}<br />
  4. 4. How to handle event?<br />When something happens(a event occurs), this method handle the event.<br />4<br />import java.awt.event.*;<br />public class ButtonHandlerimplements ActionListener{<br /> public void actionPerformed(ActionEvent e) {<br />System.out.println(“button pushed!");<br />System.out.println(e.getActionCommand());<br /> }<br />}<br />
  5. 5. Method Categories and Interfaces<br />5<br />
  6. 6. 6<br />
  7. 7. Examples<br />We want to close the window when pushing down a close button<br />How to implement? Which method do we have to use?<br />Find out event source<br />Apply addXXXListener() on the event source, define event handler<br />Implement event handler<br />7<br />
  8. 8. Examples<br />Find out event source  window<br /> What is the interface related with window? WindowListener<br />Match the event source with event handler<br />Implement WindowHandler()<br />8<br />f.addWindowListener(new WindowHandler());<br />
  9. 9. Examples<br />9<br />public class WindowHandlerimplementsWindowListener{<br /> public void windowClosing(WindowEvent e) {<br />System.exit(0); <br /> }<br />windowOpened(WindowEvent e) { }<br />windowIconified(WindowEvent e) { }<br />windowDeiconified(WindowEvent e) { }<br />windowClosed(WindowEvent e) { }<br />windowActivated(WindowEvent e) { }<br />windowDeactivated(WindowEvent e) { }<br />}<br />
  10. 10. 10<br />public class ButtonTest2 implements WindowListener {<br /> private Frame f;<br /> private Button b;<br /> public ButtonTest2(String str) {<br /> f = new Frame(str);<br />f.addWindowListener(this);<br /> b = new Button("Click!!");<br />b.addActionListener(new ButtonHandler());<br />f.add(b, "Center");<br />f.pack();<br />f.setVisible(true);<br /> }<br /> public static void main(String args[]) {<br /> ButtonTest2 fa= new ButtonTest2("Button Test");<br /> }<br /> public void windowClosing(WindowEvent e) {<br />f.setVisible(false); <br />f.dispose(); <br />System.exit(0); <br /> }<br /> public void windowOpened(WindowEvent e) { }<br /> public void windowIconified(WindowEvent e) { }<br /> public void windowDeiconified(WindowEvent e) { }<br /> public void windowClosed(WindowEvent e) { }<br /> public void windowActivated(WindowEvent e) { }<br /> public void windowDeactivated(WindowEvent e) { }<br />}<br />
  11. 11. 11<br />public class ButtonTest extends Frame implements WindowListener {<br />public ButtonTest(String str) {<br />super(str);<br />addWindowListener(this);<br />}<br />public static void main(String args[]) {<br />ButtonTestbt= new ButtonTest("Button Test");<br />Button b = new Button("Click!!");<br />b.addActionListener(new ButtonHandler());<br />bt.add(b, "Center");<br />bt.pack();<br />bt.setVisible(true);<br />}<br />public void windowClosing(WindowEvent e) {<br />setVisible(false); <br />dispose(); <br />System.exit(0); <br />}<br />public void windowOpened(WindowEvent e) { }<br />public void windowIconified(WindowEvent e) { }<br />public void windowDeiconified(WindowEvent e) { }<br />public void windowClosed(WindowEvent e) { }<br />public void windowActivated(WindowEvent e) { }<br />public void windowDeactivated(WindowEvent e) { }<br />}<br />import java.awt.event.*;<br />public class ButtonHandler implements ActionListener{<br />public void actionPerformed(ActionEvent e) {<br />System.out.println("button pushed!");<br />System.out.println(e.getActionCommand());<br />}<br />}<br />
  12. 12. Multiple Listener<br />12<br /><ul><li>We can add multiple event handlers on the one event source
  13. 13. Sensing moving, and being clocked.
  14. 14. MouseMotionListener / MouseListener interface</li></ul>Define a event source<br />Add each event handler on the event source<br />Implement two Event Handler<br />
  15. 15. public class TwoListen implements MouseMotionListener, MouseListener { <br />private Frame f; <br />private TextFieldtf;<br />public static void main(String args[]) { <br />TwoListen two = new TwoListen(); <br />two.go(); <br />} <br />public void go() { <br />f = new Frame("Two listeners example"); <br />f.add (new Label ("Click and drag the mouse"), BorderLayout.NORTH); <br />tf = new TextField (30);<br />f.add (tf, BorderLayout.SOUTH);<br />f.addMouseMotionListener(this);<br />f.addMouseListener (this);<br />f.setSize(300, 200);<br />f.setVisible(true); <br />}<br />... <br />...<br />1<br />Event source<br />Add listeners<br />2<br />
  16. 16. ... <br />...<br />public void mouseDragged (MouseEvent e) { <br />String s = "Mouse dragging: X=“+e.getX()+" Y=“+ e.getY(); <br />tf.setText (s); <br />} <br />public void mouseEntered (MouseEvent e) { <br />String s = "The mouse entered"; <br />tf.setText (s);<br />} <br />public void mouseExited (MouseEvent e) { <br />String s = "The mouse has left the building"; <br />tf.setText (s); <br />} <br />public void mouseMoved (MouseEvent e) { }<br />public void mousePressed (MouseEvent e) { }<br />public void mouseClicked (MouseEvent e) { }<br />public void mouseReleased (MouseEvent e) { } <br />} <br />Implement all abstract methods in Listeners<br />3<br />

×