package VTest2;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;

public class WindowDragSize {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final JFrame frame = new JFrame();
		frame.setUndecorated(true);
		  Border b = new CompoundBorder(new EtchedBorder(),new LineBorder(Color.RED));
          final JPanel panel = new JPanel(){
				@Override
		        public void paintComponent(Graphics g) {
		            ImageIcon icon =
		                    new ImageIcon("C:\\Users\\asus\\Desktop\\新建文件夹\\6.jpg");
		            // 图片随窗体大小而变化
		            g.drawImage(icon.getImage(), 0, 0,
		            		frame.getSize().width,frame.getSize().height,frame);
		        }};
		  panel.setSize(200,100);
		  panel.setBorder(b);
		  
		  
		  panel.addMouseMotionListener(new MouseAdapter() {
			    private boolean top = false;
			    private boolean down = false;
			    private boolean left = false;
			    private boolean right = false;
			    private boolean drag = false;
			    private Point lastPoint = null;
			    private Point draggingAnchor = null;
	            @Override
	            public void mouseMoved(MouseEvent e) {
	            	if(  e.getPoint().getY() == 0 ){
	            		frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
	            		top = true;
	            	}else if(Math.abs(e.getPoint().getY()- frame.getSize().getHeight()) <=5 ){
	            		frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
	            		down = true;
	            	}else if(e.getPoint().getX() == 0 ){
	            		frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
	            		left = true;
	            	}else if(Math.abs(e.getPoint().getX()- frame.getSize().getWidth()) <=5 ){
	            		frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
	            		right = true;
	            	}else{
	            		frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	            		draggingAnchor = new Point(e.getX() + panel.getX(), e.getY() + panel.getY());
	            		top = false;
	            		down = false;
	            		left = false;
	            		right = false;
	            		drag = true;
	            	}
	  
	            }
	            
	            @Override
	            public void mouseDragged(MouseEvent e) {
	            	Dimension dimension = frame.getSize();
	            	if(top){
	            		
	            		dimension.setSize(dimension.getWidth() ,dimension.getHeight()-e.getY());
	            		frame.setSize(dimension);
	            		frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y + e.getY());
	            	}else if(down){
	              
	            		dimension.setSize(dimension.getWidth() , e.getY());
	            		frame.setSize(dimension);
	      
	            	}else if(left){
	            	 
	            		dimension.setSize(dimension.getWidth() - e.getX() ,dimension.getHeight() );
	            		frame.setSize(dimension);
	            		
	     
	            		frame.setLocation(frame.getLocationOnScreen().x + e.getX(),frame.getLocationOnScreen().y );
	                
	            	}else if(right){
	            	 
	            		dimension.setSize(e.getX(),dimension.getHeight());
	            		frame.setSize(dimension);
	            	}else {	
	                    frame.setLocation(e.getLocationOnScreen().x - draggingAnchor.x, e.getLocationOnScreen().y - draggingAnchor.y);
	            	}
	            }
	        });

		  frame.setLocation(200,100);
		  frame.getContentPane().add(panel);
		  frame.setSize(200,100);
		  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  frame.setAlwaysOnTop(true);
		  frame.show();
		  
	}

} 
最近下载更多
qq1654486219  LV6 2020年5月3日
wei112233  LV15 2020年4月2日
bluecatnmd  LV1 2019年5月20日
刘花花  LV1 2018年12月26日
省公司管理员  LV2 2018年8月30日
ZhengGe666  LV2 2018年4月1日
笑瘾iiiii  LV2 2017年12月10日
chencs  LV4 2017年11月14日
喵酱Yuka  LV1 2017年9月11日
sankin  LV1 2017年7月8日
最近浏览更多
一起吹过晚风的街  LV1 2022年5月12日
体验放地上  LV1 2022年3月15日
本宝宝  LV2 2021年6月22日
coderliu 2021年6月21日
暂无贡献等级
lizhenlinok  LV10 2021年1月10日
lu540606  LV1 2020年12月22日
675104182  LV14 2020年9月22日
输入的V领会计  LV2 2020年8月12日
527826533  LV10 2020年8月10日
xujingfong  LV1 2020年7月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友