Java implements mouse drag and drop to move interface components

  • 2020-04-01 03:28:48
  • OfStack

By default, Frame or JFrame itself already implements the ability to drag and drop the title bar to move the window.

However, if you don't like Java's JFrame style, hide the title bar and borders, or use the JWindow directly, how do you move the window by dragging and dropping? At first, I simple frame in the mouseDragged method setLocation (um participant etX (), um participant etY ()), as a result, the frame dragging when kept flashing, location on the screen continuously. Later look up information on the Internet, found the answer.

  Here's a simple example that makes sense:


 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;
 
 
 public class MyFrame extends JWindow {
 
   private static final long serialVersionUID = 1L;
   
   JLabel titleLbl;
   
   public MyFrame() {
     //Setting the background color does not directly call its setBackground method, but sets the background color of its ContentPane.
     this.getContentPane().setBackground(new Color(0x99FF66));
     this.setBounds(100,100,600,400);
     this.setLayout(null);
     
     titleLbl = new JLabel("  Customize the 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);
   }
 
   
   class MouseEventListener implements MouseInputListener {
     
     Point origin;
     //Drag and drop the target component you want to move
     MyFrame frame;
     
     public MouseEventListener(MyFrame frame) {
       this.frame = frame;
       origin = new Point();
     }
     
     @Override
     public void mouseClicked(MouseEvent e) {}
 
     
     @Override
     public void mousePressed(MouseEvent e) {
       origin.x = e.getX(); 
       origin.y = e.getY();
     }
 
     @Override
     public void mouseReleased(MouseEvent e) {}
 
     
     @Override
     public void mouseEntered(MouseEvent e) {
       this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
     }
     
     
     @Override
     public void mouseExited(MouseEvent e) {
       this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
     }
 
     
     @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 articles: