Specifications

Sun Services
Java™ Programming Language
Module 11, slide 22 of 22
Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F
Event Handling Using Anonymous Classes
18 public void launchFrame() {
19 Label label = new Label("Click and drag the mouse");
20 // Add components to the frame
21 f.add(label, BorderLayout.NORTH);
22 f.add(tf, BorderLayout.SOUTH);
23 // Add a listener that uses an anonymous class
24 f.addMouseMotionListener(new MouseMotionAdapter() {
25 public void mouseDragged(MouseEvent e) {
26 String s = "Mouse dragging: X = "+ e.getX()
27 + " Y = " + e.getY();
28 tf.setText(s);
29 }
30 }); // <- note the closing parenthesis
31 f.addMouseListener(new MouseClickHandler()); // Not shown
32 // Size the frame and make it visible
33 f.setSize(300, 200);
34 f.setVisible(true);
35 }
36 }