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}