<ul><ul><ul><ul><ul><li>GUI Event Handling   </li></ul></ul></ul></ul></ul>
<ul><li>Objectives </li></ul><ul><ul><li>Define events and event handling  </li></ul></ul><ul><ul><li>Write code to handle...
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event  Event handlers –...
Delegation Model  An event can be sent to many event handlers Event handlers register with components when they are intere...
Delegation Model import java.awt.*; public class TestButton { private Frame f; private Button b; public TestButton() { f =...
<ul><li>import java.awt.event.*; </li></ul><ul><li>public class ButtonHandler implements ActionListener { </li></ul><ul><l...
 
 
 
<ul><li>Event Adapters </li></ul><ul><li>The listener classes that you define can extend  </li></ul><ul><li>adapter classe...
Upcoming SlideShare
Loading in...5
×

Md11 gui event handling

352

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
352
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Md11 gui event handling"

  1. 1. <ul><ul><ul><ul><ul><li>GUI Event Handling </li></ul></ul></ul></ul></ul>
  2. 2. <ul><li>Objectives </li></ul><ul><ul><li>Define events and event handling </li></ul></ul><ul><ul><li>Write code to handle events that occur in a GUI </li></ul></ul><ul><ul><li>Event Categories </li></ul></ul><ul><ul><li>Java GUI Behavior </li></ul></ul><ul><ul><li>Describe the concept of adapter classes, including how </li></ul></ul><ul><ul><li>and when to use them </li></ul></ul>
  3. 3. What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event object, deciphers it, and processes the user’s interaction
  4. 4. Delegation Model An event can be sent to many event handlers Event handlers register with components when they are interested in events generated by that component
  5. 5. Delegation Model import java.awt.*; public class TestButton { private Frame f; private Button b; public TestButton() { f = new Frame(&quot;Test&quot;); b = new Button(&quot;Press Me!&quot;); b.setActionCommand(&quot;ButtonPressed&quot;); } public void launchFrame() { b.addActionListener(new ButtonHandler()); f.add(b,BorderLayout.CENTER); f.pack(); f.setVisible(true); } public static void main(String args[]) { TestButton guiApp = new TestButton(); guiApp.launchFrame(); } }
  6. 6. <ul><li>import java.awt.event.*; </li></ul><ul><li>public class ButtonHandler implements ActionListener { </li></ul><ul><li>public void actionPerformed(ActionEvent e){ </li></ul><ul><ul><ul><ul><ul><li>System.out.println(&quot;Action occurred&quot;); System.out.println(&quot;Button’s command is: &quot; + e.getActionCommand()) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} } </li></ul></ul></ul></ul></ul>
  7. 10. <ul><li>Event Adapters </li></ul><ul><li>The listener classes that you define can extend </li></ul><ul><li>adapter classes and override only the methods that you need </li></ul><ul><li>Example: </li></ul><ul><li>import java.awt.*; import java.awt.event.*; public class MouseClickHandler extends MouseAdapter { // We just need the mouseClick handler, so we use // the an adapter to avoid having to write all the // event handler methods public void mouseClicked(MouseEvent e) </li></ul><ul><li> // Do stuff with the mouse click... } } </li></ul>

×