1 / 31

An example that uses inner classes

Learn how to handle events in Java using inner classes. This example focuses on event handling in GUI programming with AWT and Swing components.

woodst
Télécharger la présentation

An example that uses inner classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week Four Continued An example that uses inner classes 95-713 Intermediate Java

  2. Event Handling • Used by the Abstract Window Toolkit (AWT) – for basic GUI • programming Java 1.0 • Used by Swing -- Better components than AWT Java 2 • Used by JavaBeans -- reusable software components, like • Visual Basic, that can be manipulated • in a builder tool 95-713 Intermediate Java

  3. Babysitting A baby in the home needs attention. Many events surrounding the baby are not easily ignored. Events can be of different types. 95-713 Intermediate Java

  4. One possible sourceof events 95-713 Intermediate Java

  5. Babysitting Before responding to an event, a babysitter typically takes note of the source and the type of event. 95-713 Intermediate Java

  6. Babysitting Handler Event type Event source The sitter needs to know both the event type and the event source. 95-713 Intermediate Java

  7. Event Handling The window manager software may generate hundreds of different events. Examples include mouse clicks, mouse movements, key strokes, and timer ticks. A programmer is typically interested in a small subset of all of the possible events that may occur. 95-713 Intermediate Java

  8. Events and Event Objects • Since events may be of different types but still exhibit some • shared traits (inheritance) Java represents the event classes • in a hierarchy. • The root class is called java.util.EventObject. The only common • feature shared by all events is a source object. So we find the • following method in the EventObject class : • public Object getSource(); 95-713 Intermediate Java

  9. Object The Event Type is described by these Classes. EventObject public Object getSource(); AWTEvent ActionEvent ComponentEvent String getActionCommand() WindowEvent InputEvent boolean isAltDown() Window getWindow() MouseEvent KeyEvent int getX() char getKeyChar() 95-713 Intermediate Java

  10. ComponentEvent • - A low-level event which indicates that a component moved, changed size, or changed visibility • For notification only • The AWT will automatically handle component moves and resizes internally so that GUI layout works properly regardless of whether a program is receiving these events or not. 95-713 Intermediate Java

  11. ActionEvent • Indicates that a component-defined action occured • High-level event is generated by a component (such as a Button) • when the component-specific action occurs • (such as being pressed). 95-713 Intermediate Java

  12. Event handling usually involves three types of objects • Objects are used to hold and report on information about the event. • Objects are typically the source of events. • Objects, called listeners, are used to handle events. 95-713 Intermediate Java

  13. A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked Event type object has data and methods Event Source Listener The listener object has methods that are called for particular events 95-713 Intermediate Java

  14. A mouse object must be told who its listener is. A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked Implements MouseListener The listener object has methods that are called for particular events The event object is sent to a listener method 95-713 Intermediate Java

  15. The Listeners • There are different types of Listeners. • MouseListeners • WindowListeners • ScrollBarListeners • Etc.. • These Listeners are interfaces. Remember what an interface • provides? If class X implements an interface then class X promises • to provide (at least) the methods declared in the interface. 95-713 Intermediate Java

  16. Some of the Listener InterfaceHierarchy A tagging interface with no methods. EventListener KeyListener ActionListener MouseListener abstract void actionPerformed(ActionEvent e); abstract void mouseClicked(MouseEvent e) abstarct public void keyTyped(KeyEvent e) 95-713 Intermediate Java

  17. A Listener Interface public interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mouseReleased(MouseEvent e); } What does it mean if I claim that I implement this interface? 95-713 Intermediate Java

  18. An Example of creating a listener // MouseSpyApplet.java import java.applet.Applet; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; Applets can be an event source 95-713 Intermediate Java

  19. class MouseSpy implements MouseListener { public void mouseClicked(MouseEvent event) { System.out.println("Mouse clicked. x = " + event.getX() + " y = " + event.getY()); } public void mouseEntered(MouseEvent event) { System.out.println("Mouse entered. x = " + event.getX() + " y = " + event.getY()); } public void mouseExited(MouseEvent event) { System.out.println("Mouse exited. x = " + event.getX() + " y = " + event.getY()); } public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed. x = " + event.getX() + " y = " + event.getY()); } public void mouseReleased(MouseEvent event) { System.out.println("Mouse released. x = " + event.getX() + " y = " + event.getY()); } } Since we are implementing the MouseListener interface we must provide all of these methods!! But, at least now we have a listener. 95-713 Intermediate Java

  20. Create a listener. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } } Tell the applet who to call with any MouseEvent objects. 95-713 Intermediate Java

  21. Java Applets need an HTML file. <html> <head> </body> </head> <B>Spying on the mouse</b><p> <applet code="MouseSpyApplet.class" width=400 height=200> </applet> </html> 95-713 Intermediate Java

  22. Creating a Listener Another approach Suppose a friendly sole created this class: public class MouseAdapter implements MouseListener { void mouseClicked(MouseEvent e){} void mouseEntered(MouseEvent e){} void mouseExited(MouseEvent e){} void mouseReleased(MouseEvent e){} } Now, suppose we extend this class. What must we provide? Note the empty bodies. 95-713 Intermediate Java

  23. …only those methods that we are interested in. The other methods, when called, do nothing. Java provides several classes called “Adapter” classes for this purpose. There are WindowAdapters, MouseMotionAdapters… 95-713 Intermediate Java

  24. Register the listener Suppose we have a Button object Button b = new Button(); We must determine what events a particular component generates. We can see from the documentation that the Button class has an addActionListener() method. We resister the listener through the addActionListener method. Button b ActionListener addActionListener() ActionEvent Object 95-713 Intermediate Java

  25. Register The Listener • We want to listen for the ActionEvent object coming from the button. • So, we tell the button what object will listen for the ActionEvent object: • b.addActionListener(sitter) 95-713 Intermediate Java

  26. Once again but with a window Hit the X and the program exits 95-713 Intermediate Java

  27. Create a WindowCloseSitter Class import javax.swing.*; import java.awt.event.*; public class CloseDemo { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); WindowCloseSitter h = new WindowCloseSitter(); f.addWindowListener(h); } } 95-713 Intermediate Java

  28. But we have to implement ALL of the functions class WindowCloseSitter implements WindowListener { public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } 95-713 Intermediate Java

  29. Let’s use The WindowAdapter class public abstract class WindowAdapter implements WindowListener { public void windowClosing(WindowEvent e) {} public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } A built in class -- we already have the empty bodies!! 95-713 Intermediate Java

  30. The Window again import javax.swing.*; import java.awt.event.*; public class CloseDemo2 extends WindowAdapter { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); f.addWindowListener(new CloseDemo2()); } public void windowClosing(WindowEvent e) { System.exit(0); } } 95-713 Intermediate Java

  31. Again with anonymous classes import javax.swing.*; import java.awt.event.*; public class CloseDemo3 { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } } 95-713 Intermediate Java

More Related