devxlogo

Implement mouseDragged and mouseMove Events

Implement mouseDragged and mouseMove Events

This tip shows you how to implement mouseDragged and mouseMove events for moving components into a frame:

import java.awt.*;import java.awt.event.*;class f extends Frame implements MouseMotionListener,MouseListener{Checkbox CB=new Checkbox("Label",true);Button BP=new Button("BUTTON_1");Button B=new Button("BUTTON");TextField TF=new TextField(10);Panel P=new Panel();List TA=new List();boolean b1=false;int c1=0;int c2=0;public f(String titlu)       {       super(titlu);       }void init()      {      setLayout(null);             TA.addMouseMotionListener(this);      TA.addMouseListener(this);      B.addMouseMotionListener(this);      B.addMouseListener(this);      P.addMouseMotionListener(this);      P.addMouseListener(this);      CB.addMouseMotionListener(this);      CB.addMouseListener(this);      TA.setLocation(50,50);      TA.setSize(100,100);TA.add("List");      B.setLocation(100,150);B.setSize(80,40);      CB.setLocation(120,370);CB.setSize(100,20);      P.setLayout(new BorderLayout());      P.setLocation(100,200);P.setSize(150,150);      P.setBackground(Color.yellow);      P.add(BP,BorderLayout.NORTH);      P.add(TF,BorderLayout.SOUTH);      add(TA);add(B);add(P);add(CB);     setLocation(0,0);	     setSize(400,400);     setVisible(true);     //show();     } //mouseDraggedpublic void mouseDragged(MouseEvent e)             {                         Component C=e.getComponent();             if(b1==false){b1=true;Point p=new                              Point(e.getPoint());c1=p.x;c2=p.y;}                     Point z=new Point(e.getPoint());             Point q=new Point(C.getLocation());                            C.setBounds(q.x+(z.x-c1),q.y+(z.y-c2),C.getSize().                                       width,C.getSize().height);               } //mouseReleasedpublic void mouseReleased(MouseEvent e)              {              b1=false;              }          //mouseEnteredpublic void mouseEntered(MouseEvent e)              {              Cursor c1=new Cursor(Cursor.HAND_CURSOR);                                                  setCursor(c1);              } //mouseExitedpublic void mouseExited(MouseEvent e)              {              Cursor c2=new Cursor(Cursor.DEFAULT_CURSOR);                                                  setCursor(c2);              }               //mouseClickedpublic void mouseClicked(MouseEvent e)              {              Component C=e.getComponent();              System.out.println(C);              }                          //mouseMoved public void mouseMoved(MouseEvent e)              {              Component C=((Component)e.getSource());              java.util.Random r=new java.util.Random();              java.util.Random g=new java.util.Random();              java.util.Random b=new java.util.Random();              int cr=r.nextInt(255);int cg=g.nextInt(255);              int cb=b.nextInt(255);              Color col=new Color(cr,cg,cb);              C.setBackground(col);              } //mousePressedpublic void mousePressed(MouseEvent e){}                              }public class mouse_events{       public static void main(String[] args)             {             f t=new f("MOUSE EVENT");             t.init();             }       }
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist