By default, Frame or JFrame itself has implemented the function of moving the window by dragging the title bar with the mouse.
However, when you are not satisfied with Java's JFrame style, hide the title bar and borders, or simply use JWindow directly, then how can you achieve the purpose of moving the window by dragging the mouse? At first, I simply used frame.setLocation(e.getX(), e.getY()) in the mouseDragged method. As a result, the frame kept flickering when I dragged it, and the position kept jumping on the screen. Later, I looked up information online and found the answer.
Here is a simple example that will be easy to understand at a glance:
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; /** * Customize the program window, and the mouse can be dragged to move its position. * @author Jeby Sun * */ public class MyFrame extends JWindow { private static final long serialVersionUID = 1L; JLabel titleLbl; public MyFrame() { //To set the background color, you cannot directly call its setBackground method, but set the background color of its ContentPane . this.getContentPane().setBackground(new Color(0x99FF66)); this.setBounds(100,100,600,400); this.setLayout(null); titleLbl = new JLabel("Customized 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; //Drag the target component you want to move with the mouse MyFrame frame; public MouseEventListener(MyFrame frame) { this.frame = frame; origin = new Point(); } @Override public void mouseClicked(MouseEvent e) {} /** * Record the point when the mouse is pressed*/ @Override public void mousePressed(MouseEvent e) { origin.x = e.getX(); origin.y = e.getY(); } @Override public void mouseReleased(MouseEvent e) {} /** * When the mouse moves into the title bar, set the mouse icon to the mobile icon*/ @Override public void mouseEntered(MouseEvent e) { this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); } /** * When the mouse moves out of the title bar, set the mouse icon as the default pointer */ @Override public void mouseExited(MouseEvent e) { this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } /** * Drag the mouse on the title bar When dragging, set the coordinate position of the window * the new coordinate position of the window = the coordinate position before moving + (the current coordinates of the mouse pointer - the position of the pointer when the mouse is pressed) */ @Override public void mouseDragged(MouseEvent e) { Point p = this.frame.getLocation(); this.frame.setLocation( px + (e.getX() - origin.x), py + (e.getY() - origin. y)); } @Override public void mouseMoved(MouseEvent e) {} } public static void main(String[] args) { new MyFrame(); } }