91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

基于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

民和| 无锡市| 盐津县| 广灵县| 城市| 大埔县| 太湖县| 洱源县| 郑州市| 波密县| 六安市| 岑巩县| 天津市| 衢州市| 甘洛县| 和政县| 乌海市| 牙克石市| 宜春市| 枝江市| 独山县| 长阳| 体育| 会宁县| 离岛区| 连山| 秭归县| 武陟县| 吉林省| 论坛| 寿光市| 潍坊市| 吴忠市| 台前县| 密云县| 淮北市| 双柏县| 朝阳区| 交城县| 蓬莱市| 新兴县|