26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

734 Graphical User Interface Components: Part 2 Chapter 13<br />

28 container.setLayout( new FlowLayout() );<br />

29 container.add( myPanel );<br />

30<br />

31 // set up mouse motion event handling<br />

32 addMouseMotionListener(<br />

33<br />

34 // anonymous inner class for mouse motion event handling<br />

35 new MouseMotionListener() {<br />

36<br />

37 // handle mouse drag event<br />

38 public void mouseDragged( MouseEvent event )<br />

39 {<br />

40 setTitle( "Dragging: x=" + event.getX() +<br />

41 "; y=" + event.getY() );<br />

42 }<br />

43<br />

44 // handle mouse move event<br />

45 public void mouseMoved( MouseEvent event )<br />

46 {<br />

47 setTitle( "Moving: x=" + event.getX() +<br />

48 "; y=" + event.getY() );<br />

49 }<br />

50<br />

51 } // end anonymous inner class<br />

52<br />

53 ); // end call <strong>to</strong> addMouseMotionListener<br />

54<br />

55 setSize( 300, 200 );<br />

56 setVisible( true );<br />

57 }<br />

58<br />

59 // execute application<br />

60 public static void main( String args[] )<br />

61 {<br />

62 SelfContainedPanelTest application =<br />

63 new SelfContainedPanelTest();<br />

64<br />

65 application.setDefaultCloseOperation(<br />

66 JFrame.EXIT_ON_CLOSE );<br />

67 }<br />

68<br />

69 } // end class SelfContainedPanelTest<br />

Fig. Fig. 13.5 13.5 Capturing mouse events with a JPanel (part 2 of 3).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!