English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementación de arrastrar y estirar ventanas basada en swing

Este artículo comparte el código específico para implementar el arrastrar y estirar de la ventana utilizando Swing, a modo de ejemplo, y el contenido específico es el siguiente

Después de usar setUndecorated(true), cuando JFrame elimina la barra de título, debe escribir la funcionalidad de arrastrar y estirar por sí mismo.

A continuación, se muestra la imagen del efecto, ya que mi software de captura de pantalla no puede capturar punteros diferentes al puntero predeterminado del sistema, por lo que los cambios en los punteros en todas las direcciones no se muestran en la imagen.

El código es el siguiente:

import javax.swing.*; 
import java.awt.*; 
/** 
 * Arrastrar y estirar la ventana 
 */ 
public class winReSizeDemo { 
 private JFrame jf; 
 public winReSizeDemo(){ 
  jf=new JFrame(); 
  jf.setUndecorated(true);//Quitar la barra de título y las barras de borde 
  jf.setLocationRelativeTo(null);//Centrar la ventana 
  jf.setSize(400,400); 
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  reSizeEvent dg = new reSizeEvent(jf); 
  /**Agregar dos oyentes**/ 
  jf.addMouseListener(dg); 
  jf.addMouseMotionListener(dg); 
  jf.setVisible(true); 
 } 
 public static void main(String [] args){ 
  new winReSizeDemo(); 
 } 
} 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
/** 
 * Implementar el estiramiento y el arrastre en todas las direcciones de la ventana. 
 */ 
public class reSizeEvent extends MouseAdapter{ 
 public JFrame jf; 
 private Point prePos,curPos,jfPos; 
 private static final double BREADTH = 15.0;//边界拉伸范围 
 private int dragType; 
 private static final int DRAG_MOVE = 1; 
 private static final int DRAG_UP = 2; 
 private static final int DRAG_UPLEFT = 3; 
 private static final int DRAG_UPRIGHT = 4; 
 private static final int DRAG_LEFT = 5; 
 private static final int DRAG_RIGHT = 6; 
 private static final int DRAG_BOTTOM = 7; 
 private static final int DRAG_BOTTOMLEFT = 8; 
 private static final int DRAG_BOTTOMRIGHT = 9; 
 public reSizeEvent(JFrame jf){ 
  this.jf = jf; 
 } 
 @Override 
 public void mousePressed(MouseEvent e){ 
  prePos = e.getLocationOnScreen(); 
 } 
 @Override 
 public void mouseMoved(MouseEvent e){ 
  areaCheck(e.getPoint()); 
 } 
 @Override 
 public void mouseDragged(MouseEvent e){ 
  curPos = e.getLocationOnScreen(); 
  jfPos = jf.getLocation(); 
  dragAction(); 
  prePos = curPos; 
 } 
 private void dragAction(){ 
  switch(dragType){ 
   case DRAG_MOVE: 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y+curPos.y-prePos.y); 
    break; 
   case DRAG_UP://x位置不变,y位置变化,并且Height变化 
    jf.setLocation(jfPos.x, 
      jfPos.y+curPos.y-prePos.y); 
    jf.setSize(jf.getWidth(), jf.getHeight()-(curPos.y-prePos.y)); 
    break; 
   case DRAG_LEFT://y位置不变,x位置变化,width变化 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()-(curPos.x-prePos.x), jf.getHeight()); 
    break; 
   case DRAG_RIGHT://x,y位置不变,width变化 
    jf.setLocation(jfPos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()+(curPos.x-prePos.x), jf.getHeight()); 
    break; 
   case DRAG_BOTTOM://x,y位置不变,Height变化 
    jf.setLocation(jfPos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth(), jf.getHeight()+(curPos.y-prePos.y)); 
    break; 
   case DRAG_UPLEFT://x,y位置均变化,h,w均变化 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y+curPos.y-prePos.y); 
    jf.setSize(jf.getWidth()-(curPos.x-(prePos.x), jf.getHeight()-(curPos.y-prePos.y)); 
    break; 
   case DRAG_BOTTOMRIGHT://x,y位置均不变,h,w变化 
    jf.setLocation(jfPos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()+(curPos.x-(prePos.x), jf.getHeight()+(curPos.y-prePos.y)); 
    break; 
   case DRAG_UPRIGHT://x位置不变,y,w,h变化 
    jf.setLocation(jfPos.x, 
      jfPos.y+curPos.y-prePos.y); 
    jf.setSize(jf.getWidth()+(curPos.x-(prePos.x), jf.getHeight()-(curPos.y-prePos.y)); 
    break; 
   case DRAG_BOTTOMLEFT://y不变,xwh变化 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()-(curPos.x-(prePos.x), jf.getHeight()+(curPos.y-prePos.y)); 
    break; 
   default: 
    break; 
  } 
 } 
 private boolean areaCheck(Point p){ 
  if(p.getX()<=BREADTH && p.getY()<=BREADTH){ 
   dragType = DRAG_UPLEFT; 
   jf.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR)); 
  && p.getX() < (jf.getWidth() 
    dragType = DRAG_BOTTOM;-BREADTH) 
    && p.getY()<=BREADTH){ 
   dragType = DRAG_UP; 
   jf.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR)); 
  }-BREADTH) && p.getY()<=BREADTH){ 
   dragType = DRAG_UPRIGHT; 
   jf.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR)); 
  jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR)); 
    && p.getY()<(jf.getHeight(){-BREADTH) 
    && p.getY()>BREADTH){ 
   dragType = DRAG_LEFT; 
   jf.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); 
  }-BREADTH) 
    && p.getY()<(jf.getHeight(){-BREADTH) 
    && p.getY()>BREADTH){ 
   dragType = DRAG_RIGHT; 
   jf.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); 
  jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR)); 
    && p.getY() >= (jf.getHeight()-BREADTH)){ 
   else if(p.getX() > BREADTH 
   } 
  && p.getX() < (jf.getWidth() 
    dragType = DRAG_BOTTOM;-BREADTH) 
    && p.getY() >= (jf.getHeight()-BREADTH)){ 
   jf.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR)); 
   else if(p.getX() >= (jf.getWidth() 
  }-BREADTH) 
    && p.getY() >= (jf.getHeight()-BREADTH)){ 
   dragType = DRAG_BOTTOMRIGHT; 
   jf.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR)); 
  }else{ 
   dragType = DRAG_MOVE; 
   jf.setCursor(new Cursor(Cursor.MOVE_CURSOR)); 
   return false; 
  } 
  return true; 
 } 
} 

Esto es todo el contenido del artículo, espero que sea útil para su aprendizaje y que todos apoyen el tutorial de grito.

Declaración: el contenido de este artículo se obtiene de la red, pertenece al autor original, el contenido se contribuye y carga de manera autónoma por los usuarios de Internet, este sitio no posee los derechos de propiedad, no se ha procesado editorialmente y no asume la responsabilidad legal relevante. Si encuentra contenido sospechoso de infracción de derechos de autor, por favor envíe un correo electrónico a: notice#oldtoolbag.com (al enviar un correo electrónico, por favor reemplace # con @ para denunciar y proporcionar evidencia. Una vez verificada, este sitio eliminará inmediatamente el contenido sospechoso de infracción de derechos de autor.)

Te gustará