java悬浮窗效果

import   java.awt.*; 
import   java.awt.event.MouseEvent; 

import   javax.swing.JFrame; 
import   javax.swing.JLabel; 
import   javax.swing.event.MouseInputAdapter; 

public   class   UndecoratedFrame   extends   JFrame 

private   static   final   int   TITLE_HEIGHT   =   20; 

public   UndecoratedFrame()   throws   HeadlessException 

super(); 
setUndecorated(true); 

MouseHandler   ml   =   new   MouseHandler(); 
addMouseListener(ml); 
addMouseMotionListener(ml); 


public   UndecoratedFrame(String   title)   throws   HeadlessException 

super(title); 
setUndecorated(true); 

MouseHandler   ml   =   new   MouseHandler(); 
addMouseListener(ml); 
addMouseMotionListener(ml); 


public   Insets   getInsets() 

return   new   Insets(TITLE_HEIGHT,   1,   1,   1); 


public   void   paint(Graphics   g) 

super.paint(g); 
g.setColor(new   Color(0,   0,   128)); 
g.drawRect(0,   0,   getWidth()-1,   getHeight()-1); 
g.fillRect(0,   0,   getWidth(),   TITLE_HEIGHT); 

FontMetrics   fm   =   g.getFontMetrics(); 
g.setColor(Color.white); 
g.drawString(getTitle(),   2,   (TITLE_HEIGHT   -   fm.getHeight())   /   2   +   fm.getAscent()); 


private   class   MouseHandler   extends   MouseInputAdapter 

private   Point   point; 

public   void   mousePressed(MouseEvent   e) 

if   (e.getY()   <=   TITLE_HEIGHT)   { 
this.point   =   e.getPoint(); 



public   void   mouseDragged(MouseEvent   e) 

if   (point   !=   null)   { 
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 

Point   p   =   e.getPoint(); 
int   dx   =   p.x   -   point.x; 
int   dy   =   p.y   -   point.y; 

int   x   =   getX(); 
int   y   =   getY(); 
setLocation(x   +   dx,   y   +   dy); 



public   void   mouseReleased(MouseEvent   e) 

point   =   null; 
setCursor(Cursor.getDefaultCursor()); 



public   static   void   main(String[]   args) 

JFrame   f   =   new   UndecoratedFrame( "Undecorated   Frame "); 
f.getContentPane().add(new   JLabel( "Hello   World! ",   JLabel.CENTER),   BorderLayout.CENTER); 
f.setSize(400,   400); 
f.setLocationRelativeTo(null); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setVisible(true); 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值