Mobile Component Exercises

Source: Internet
Author: User

1 Importjava.awt.*;2 Importjava.awt.event.*;3 Importjavax.swing.*; 4 Importjavax.swing.event.*;5  Public classmovetext{6 Label MyText;7 Frame MT;8Point point =NewPoint (0, 0);9      PublicMoveText () {Tenmytext=NewLabel ("Move Me please!"); OneMT =NewFrame ("MoveText"); A Mytext.setbackground (color.yellow); -   -Mt.addwindowlistener (NewMywindowlistener ()); theMt.setlayout (NewFlowLayout (Flowlayout.center, 10, 10)); -Mt.setsize (600, 600); -Mt.setlocation (100, 100); -Mytext.addmousemotionlistener (NewmyMouseListener1 ()); +Mytext.addmouselistener (NewMyMouseListener2 ()); -Mytext.setsize (20, 5); + Mt.add (myText); A          atMt.setvisible (true); -     } -     classMyMouseListener1extendsMousemotionadapter { -            -          /*  - The event is triggered when the mouse is dragged. Record where the mouse is pressed (start dragging).  in          */   -          Public voidmousedragged (MouseEvent e) { to             //Converts the coordinate system to the coordinates of the new coordinate (mouse relative to the component's coordinates) to the parent component of the relative component +Point Newpoint =Swingutilities.convertpoint (MyText, E - . GetPoint (), mytext.getparent ());  the             //set a new location for a label *              $ mytext.setlocation (Mytext.getx ()Panax Notoginseng+ (Newpoint.x-point.x), mytext.gety () -+ (Newpoint.y-point.y)); the              +             //At this point, the way to start writing, obviously and the above method is very different A             //mytext.setlocation (newpoint); the              +             //change the coordinate source point -Point =Newpoint;  $         }   $     } -     classMyMouseListener2extendsmouseadapter{ -         /* the This event is triggered when the mouse is pressed. Record where the mouse is pressed (start dragging).  -          */  Wuyi          Public voidmousepressed (MouseEvent e) { the             //get the current coordinate point -Point =Swingutilities.convertpoint (MyText, E.getpoint (), Wu mytext.getparent ());  -        } About     } $      Public Static voidMain (String args[]) { -MoveText mt=NewMoveText (); -          -     } A } +  the classMywindowlistenerextendswindowadapter{ -      Public voidWindowclosing (WindowEvent e) {//how frame is closed $System.exit (0); the     } the}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.