Drag and move a frame from its content area : JFrame Window « Swing « Java Tutorial






import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
  public void paintComponent(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(300, 280));

    Main ch = new Main();
    frame.getContentPane().add(ch);
    frame.setUndecorated(true);

    MoveMouseListener mml = new MoveMouseListener(ch);
    ch.addMouseListener(mml);
    ch.addMouseMotionListener(mml);

    frame.pack();
    frame.setVisible(true);
  }
}

class MoveMouseListener implements MouseListener, MouseMotionListener {
  JComponent target;
  Point start_drag;
  Point start_loc;

  public MoveMouseListener(JComponent target) {
    this.target = target;
  }

  public static JFrame getFrame(Container target) {
    if (target instanceof JFrame) {
      return (JFrame) target;
    }
    return getFrame(target.getParent());
  }

  Point getScreenLocation(MouseEvent e) {
    Point cursor = e.getPoint();
    Point target_location = this.target.getLocationOnScreen();
    return new Point((int) (target_location.getX() + cursor.getX()),
        (int) (target_location.getY() + cursor.getY()));
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
    this.start_drag = this.getScreenLocation(e);
    this.start_loc = this.getFrame(this.target).getLocation();
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseDragged(MouseEvent e) {
    Point current = this.getScreenLocation(e);
    Point offset = new Point((int) current.getX() - (int) start_drag.getX(),
        (int) current.getY() - (int) start_drag.getY());
    JFrame frame = this.getFrame(target);
    Point new_location = new Point(
        (int) (this.start_loc.getX() + offset.getX()), (int) (this.start_loc
            .getY() + offset.getY()));
    frame.setLocation(new_location);
  }

  public void mouseMoved(MouseEvent e) {
  }
}








14.80.JFrame Window
14.80.1.Creating a WindowCreating a Window
14.80.2.Display a JFrame instanceDisplay a JFrame instance
14.80.3.Resizing and positioning a JFrameResizing and positioning a JFrame
14.80.4.Constants for JFrame window close actionConstants for JFrame window close action
14.80.5.Sizing Windows with ToolkitSizing Windows with Toolkit
14.80.6.Centering a WindowCentering a Window
14.80.7.Extending JFrame: Closing Frames by DefaultExtending JFrame: Closing Frames by Default
14.80.8.Exiting an Application When a JFrame Is Closed
14.80.9.JFrame with Label and Window Listener to Handle Closing the FrameJFrame with Label and Window Listener to Handle Closing the Frame
14.80.10.Handling JFrame Events
14.80.11.Specifying Window DecorationsSpecifying Window Decorations
14.80.12.Set Default window close operation
14.80.13.SetIconImages for JFrame
14.80.14.Creating a Titleless and Borderless JFrame
14.80.15.Close a JFrame under condition
14.80.16.Creating a Borderless Window
14.80.17.Creating Frames with a background image
14.80.18.Setting the Icon for a Frame
14.80.19.Getting All Created Frames in an Application
14.80.20.Removing the Title Bar of a Frame
14.80.21.Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size.
14.80.22.Use Component listener to ensure frame visibilities
14.80.23.Drag and move a frame from its content area
14.80.24.Disabling the Close Button on a JFrame
14.80.25.Maximize a JFrame
14.80.26.Make a JFrame always visible
14.80.27.Preventing a Window from Gaining the Focus
14.80.28.Getting the Child Components of a Container
14.80.29.Get Top Frame
14.80.30.Make a JInternalFrame a tool window
14.80.31.Move JInternalFrame To Front