简体   繁体   中英

Mouse returns to home position when i drag

I have created a JLabel lab1. Added it to a panel and added mousemotionlistener to the label

 lab1.addMouseMotionListener(new InfoListener());

Here is the mouseDragged code

 public void mouseDragged(MouseEvent me){

            lab1=(JLabel)me.getSource();
            lab1.setLocation(me.getPoint());

    }

When i click and drag the label the position alternates between the moved point and home position of the panel and does not exactly move to where i want it to. The panel is in null layout and i have also mentioned setBounds for it initially.

What about this?

Point p=me.getPoint();

lab1.setLocation(p.x+lab1.getX(), p.y+lab1.getY());

1) code posted here talking nothing about your topic, edit your question with SSCCE demonstrating yout issue with mouseDragged(MouseEvent me)

2) please read tutorial about Drag and Drop , examples here

3) don't use AbsoluteLayout , use proper LayoutManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM