中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于swing實現窗體拖拽和拉伸

發布時間:2020-10-17 16:19:13 來源:腳本之家 閱讀:197 作者:_Thomas 欄目:編程語言

本文為大家分享了swing實現窗體拖拽和拉伸的具體代碼,供大家參考,具體內容如下

當用setUndecorated(true) 后 JFrame去掉標題欄后就得自己寫拖拽和拉伸功能了。

下面是效果圖,我的截圖軟件不能夠截取除系統默認光標外的光標,所以各個方向光標變化在圖中沒有體現

基于swing實現窗體拖拽和拉伸

代碼如下:

import javax.swing.*; 
import java.awt.*; 
 
/** 
 * 窗體拖拽和拉伸 
 */ 
public class winReSizeDemo { 
 private JFrame jf; 
 public winReSizeDemo(){ 
  jf=new JFrame(); 
  jf.setUndecorated(true);//去標邊界和標題欄 
  jf.setLocationRelativeTo(null);//窗口置中 
  jf.setSize(400,400); 
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  reSizeEvent dg = new reSizeEvent(jf); 
  /**添加兩個監聽器**/ 
  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; 
/** 
 * 實現窗口各個方向拉伸以及拖動。 
 */ 
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)); 
  }else if(p.getX()>BREADTH 
    && p.getX()<(jf.getWidth()-BREADTH) 
    && p.getY()<=BREADTH){ 
   dragType = DRAG_UP; 
   jf.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR)); 
  }else if(p.getX()>=(jf.getWidth()-BREADTH) && p.getY()<=BREADTH){ 
   dragType = DRAG_UPRIGHT; 
   jf.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR)); 
  }else if(p.getX()<=BREADTH 
    && p.getY()<(jf.getHeight()-BREADTH) 
    && p.getY()>BREADTH){ 
   dragType = DRAG_LEFT; 
   jf.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); 
  }else if(p.getX()>=(jf.getWidth()-BREADTH) 
    && p.getY()<(jf.getHeight()-BREADTH) 
    && p.getY()>BREADTH){ 
   dragType = DRAG_RIGHT; 
   jf.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); 
  }else if(p.getX()<=BREADTH 
    && p.getY()>=(jf.getHeight()-BREADTH)){ 
   dragType = DRAG_BOTTOMLEFT; 
   jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR)); 
  }else if(p.getX()>BREADTH 
    && p.getX()<(jf.getWidth()-BREADTH) 
    && p.getY()>=(jf.getHeight()-BREADTH)){ 
   dragType = DRAG_BOTTOM; 
   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; 
 } 
} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大厂| 巨鹿县| 防城港市| 吴川市| 麦盖提县| 广水市| 京山县| 琼海市| 邵武市| 塔河县| 犍为县| 普陀区| 蓝山县| 肇庆市| 昭平县| 晋宁县| 皋兰县| 广东省| 丹巴县| 哈巴河县| 张北县| 莱西市| 社旗县| 南召县| 南乐县| 长沙县| 平舆县| 苏尼特右旗| 庄浪县| 德格县| 上栗县| 翁牛特旗| 普洱| 凯里市| 乐清市| 中超| 新乡市| 鄂伦春自治旗| 太谷县| 新田县| 大关县|