A position of a window on the screen. : ComponentListener « Swing Event « Java Tutorial






import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

public class MovingWindow extends JFrame implements ComponentListener {
  public MovingWindow() {
    addComponentListener(this);
    setSize(310, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }

  public void componentResized(ComponentEvent e) {
  }

  public void componentMoved(ComponentEvent e) {
    int x = e.getComponent().getX();
    int y = e.getComponent().getY();
    System.out.println("x: " + x);
    System.out.println("y: " + y);
  }

  public void componentShown(ComponentEvent e) {
  }

  public void componentHidden(ComponentEvent e) {
  }

  public static void main(String[] args) {
    new MovingWindow();
  }
}








15.10.ComponentListener
15.10.1.How to Write a Component ListenerHow to Write a Component Listener
15.10.2.ComponentListener and ComponentEvent
15.10.3.extends ComponentAdapter
15.10.4.A ComponentAdapter.
15.10.5.A position of a window on the screen.
15.10.6.Installs/resets a ComponentListener to resize the given window to minWidth/Height if needed