Java implementation mouse drag and drop mobile interface components _java

Source: Internet
Author: User
Tags gety set background java jframe

By default, frame or JFrame itself has implemented the ability of the mouse to drag the title bar to move the window.

Just, when you are not satisfied with the Java JFrame style, hide the title bar and border, or simply use JWindow, then how do you implement the mouse drag and drop the purpose of the mobile window? In the beginning, I simply frame.setlocation in the Mousedragged method (E.getx (), E.gety ()), and as a result, the frame was constantly flashing while dragging, and the position was beating continuously on the screen. Later on the Internet to check the data, found the answer.

Here's a simple example to see:

 Package Com.jebysun.test.globalhotkey;
 Import Java.awt.Color;
 Import Java.awt.Cursor;
 Import Java.awt.Point;
 
 Import java.awt.event.MouseEvent;
 Import Javax.swing.JLabel;
 Import Javax.swing.JWindow;
 
 Import Javax.swing.event.MouseInputListener;
 /** * Custom program window, the mouse can drag and drop move its position.
   
   * @author Jeby Sun * */public class MyFrame extends JWindow {private static final long serialversionuid = 1L;
   
   JLabel Titlelbl;
     Public MyFrame () {//Set background color cannot call its SetBackground method directly, but set its contentpane background color.
     This.getcontentpane (). SetBackground (New Color (0x99ff66));
     This.setbounds (100,100,600,400);
     
     This.setlayout (NULL);
     TITLELBL = new JLabel ("Custom window title bar");
     Titlelbl.setopaque (TRUE);
     Titlelbl.setbackground (New Color (0X66CC00));
     Titlelbl.setbounds (0, 0, 600, 30);
     This.add (TITLELBL);
     Mouse event-handling class Mouseeventlistener MouseListener = new Mouseeventlistener (this);
     Titlelbl.addmouselistener (MouseListener); Titlelbl.addmousemotionlisTener (MouseListener);
   This.setvisible (TRUE);
     
     /** * Mouse Event handling * @author Jeby Sun * */class Mouseeventlistener implements Mouseinputlistener {
     Point origin;
     
     The mouse drags the target component myframe frame to move;
       Public Mouseeventlistener (MyFrame frame) {this.frame = frame;
     Origin = new Point ();
     @Override public void mouseclicked (MouseEvent e) {}/** * record the point when mouse is pressed * * @Override 
       public void mousepressed (MouseEvent e) {origin.x = E.getx ();
     ORIGIN.Y = E.gety (); @Override public void mousereleased (MouseEvent e) {}/** * mouse over the title bar, set the mouse icon to move the icon * * @Ov Erride public void mouseentered (MouseEvent e) {this.frame.setCursor (cursor.getpredefinedcursor
     Rsor)); /** * Mouse cursor out of the title bar, set the mouse icon as the default pointer/@Override public void mouseexited (MouseEvent e) {th Is.frame.setCursor (Cursor.getpredefiNedcursor (Cursor.default_cursor));  /** * Mouse in the title bar drag, set the window coordinates position * Window New coordinate position = move Front coordinate position + (mouse pointer current coordinate-mouse pointer position) */@Override Public
       void mousedragged (MouseEvent e) {point P = this.frame.getLocation (); 
     This.frame.setLocation (p.x + (E.getx ()-origin.x), P.y + (E.gety ()-ORIGIN.Y)); 
     @Override public void mousemoved (MouseEvent e) {}} public static void Main (string[] args) {
   New MyFrame ();
 }
 
 }

Related Article

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.