关闭

写一个无边框JFrame,并使其拖动

329人阅读 评论(0) 收藏 举报

package DeskTop;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Note extends JFrame implements MouseListener,MouseMotionListener{
 private JLabel iconLabel;
 private JPanel firstpPanel;
 private ImageIcon backIcon;
 public  Note() {
  backdrop();
 }
 private void backdrop() {
  //搭建背景,容器
  setUndecorated(true);
  backIcon = new ImageIcon("backdrop1.gif");
  iconLabel = new JLabel(backIcon);
  iconLabel.setBounds(0, 0,backIcon.getIconWidth(),backIcon.getIconHeight());
  firstpPanel = (JPanel)this.getContentPane();
  getLayeredPane().setLayout(null);
  getLayeredPane().add(iconLabel, new Integer(Integer.MIN_VALUE));
  firstpPanel.setOpaque(false);
  //AWTUtilities.setWindowOpacity(this, 0.5f);
  setSize(backIcon.getIconWidth(),backIcon.getIconHeight());
  /*AWTUtilities.setWindowShape(this, new RoundRectangle2D.Double( 
             0.0D, 0.0D, this.getWidth(), this.getHeight(), 80.0D, 
             30.0D));*/
  setLocation(80, 80);
  setVisible(true);
  addMouseListener(this);
  addMouseMotionListener(this);
 }
 public static void main(String[] args) {
  new Note();
 }

 
 Boolean tBoolean = false;
 Point fPoint;
 Point sPoint;
 Point tPoint;
 
 private void move() {
  Double fx,fy;
  Double sx,sy;
  Double tx,ty;
  fx = fPoint.getX(); fy = fPoint.getY();
  sx = sPoint.getX(); sy = sPoint.getY();
  tx = tPoint.getX(); ty = tPoint.getY();
  fPoint.setLocation(fx+tx-sx, fy+ty-sy);
  setLocation(fPoint);
 }
 public void mouseDragged(MouseEvent e) {
  //tBoolean = true;
  
  //System.out.println("ddddd");
  tPoint = e.getPoint();
  move();
 }
 public void mousePressed(MouseEvent e) {
  fPoint = getLocation();
  sPoint = e.getPoint();
 }
 public void mouseMoved(MouseEvent e) {
 }
 
 public void mouseClicked(MouseEvent e) {
    
 }
 public void mouseEntered(MouseEvent e) {
  
 }
 public void mouseExited(MouseEvent e) {
  
 }
 
 public void mouseReleased(MouseEvent e) {
  
 }
 

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1907次
    • 积分:87
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类