The implementation and design of dragging pictures

Source: Internet
Author: User

In Office software we can insert and delete pictures, of course, can also move pictures, this move is how to achieve it.

I believe that the students who like to study must be very interested to know, I have consulted the data, combined with just learning GUI interface design, drag and drop function to achieve, we can refer to the code, their own drag and drop function to see, very good application Oh, I also found inside the Swingutilities.convertpoint (Lblpic, E.getpoint (), lblpic.getparent ()); This can be the screenshot of the program to change to the graphical interface, hey, with empty to change, Upgrade the next version.

Package software; Import java.awt.*; Import java.awt.event.*; Import javax.swing.*; Import javax.swing.event.*; public class Dragpicturedemo extends jframe{JLabel lblpic, public Dragpicturedemo () {super ("picture can Drag"); Setdefaultcloseo Peration (Jframe.exit_on_close); Below c://aa.jpg is you want to put in the picture position (the picture should be small, otherwise will cover the entire interface) Icon image=new imageicon ("c://aa.jpg"); Lblpic=new JLabel (image); Getcontentpane (). Add (Lblpic); Dragpiclistener listener=new Dragpiclistener (); Lblpic.addmouselistener (listener); Lblpic.addmousemotionlistener (listener); SetSize (320,240); SetVisible (TRUE); public static void Main (string[] args) {Dragpicturedemo obj=new dragpicturedemo ();}//Internal listener class//mouseinputlistener if implemented Many ports can be introduced into the system class Dragpiclistener implements Mouseinputlistener {point p=new Point (0,0), public void mousedragged (Mousee Vent e)/Drag function {point newp=swingutilities.convertpoint (Lblpic, E.getpoint (), lblpic.getparent ());//Below is the new location of the picture Lblpic.setlocation (Lblpic.getx () + (newp.x-p.x), (NEWP.Y-P.Y) +lblpic.gety ()); Lblpic.geTparent (); P=NEWP; Mousemoved (MouseEvent e) {} public void mouseclicked (MouseEvent e) {} public void mousereleased (MouseEvent e) { public void mouseentered (MouseEvent e) {} public void mouseexited (MouseEvent e) {} @Override//Mouse down time public void mousepre ssed (MouseEvent e) {//TODO auto-generated Method Stub p=swingutilities.convertpoint (Lblpic, E.getpoint (), Lblpic.getparent ()); } } }

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.