無邊框窗體隨着鼠標的移動而移動,類似於qq的登錄界面

實現不邊框窗體的移動,類似於qq的登錄界面,關鍵是要在當鼠標按下時就獲得在窗口中當前的位置,代碼:
ct.addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent e){
//按下(mousePressed 不是點擊,而是鼠標被按下沒有擡起)
    int originX = e.getX();//獲得窗口當前的位置
                    int originY = e.getY();
}
    });
  然後獲得窗口相對於屏幕的當前的位置 Point p1 =this.getLocationOnScreen(); p1.getX();p1.getY();和鼠標按下移動相對於窗口的位置
public void mouseDragged(MouseEvent e) {
  //拖動(mouseDragged 指的不是鼠標在窗口中移動,而是用鼠標拖動)
p1 = new Point();
     p1 =this.getLocationOnScreen();
     //e.getX()、e.getY()是現對於窗體的位置,
     //e.getXOnScreen()、e.getYOnScreen()是相對屏幕的位置
      //設置窗口的位置
        //這是窗體移動的核心:窗口當前的位置 + 鼠標當前在窗口的位置 - 鼠標按下的時候在窗口的位置
     setLocation(p1.x+e.getX()-originX,p1.y+e.getY()-originY);
}

不懂的地方可以跟帖提問,奉上完整代碼:
/**
* 功能:登錄界面,可以實現無邊框窗體隨着鼠標的移動而移動,類似於qq的登錄界面
*
*/
package com.Login;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.*;

import com.Model.UserModel;

public class Login extends JFrame implements MouseMotionListener{

//定義組件
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
JTextField juser;
JPasswordField jpasswd;
JButton jlg,jcl;
Date d1;
SimpleDateFormat df1,df2;
String uName ;
String pwd ;

UserModel um;
boolean b ;

Point p1 ;//當鼠標按下時記錄其位置
int originX,originY;

public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}


public Login(){
//得到當前的容器
final Container ct = this.getContentPane();
this.setLayout(null);
//添加用戶名
jl1 = new JLabel("用戶名:");
jl1.setFont(new Font("宋體",Font.PLAIN,16));
jl1.setBounds(170,120,90,30);
ct.add(jl1);
//添加密碼
jl2 = new JLabel("密 碼:");
jl2.setFont(new Font("宋體",Font.PLAIN,16));

jl2.setBounds(170,160,90,30);
ct.add(jl2);

//添加文本框
juser = new JTextField();
juser.setFont(new Font("宋體",Font.PLAIN,12));
juser.setBounds(230,127, 100, 20);
//設置文本框爲 下凹 的
juser.setBorder(BorderFactory.createLoweredBevelBorder());
ct.add(juser);
jpasswd = new JPasswordField(10);
jpasswd.setFont(new Font("宋體",Font.PLAIN,12));
jpasswd.setBounds(230,167,100,20);
//設置文本框爲 下凹 的
jpasswd.setBorder(BorderFactory.createLoweredBevelBorder());
ct.add(jpasswd);
//
juser.addActionListener(this);
jpasswd.addActionListener(this);


//添加按鈕
//jlg = new JButton("登陸");
jlg.setForeground(Color.blue);//設置前景色(字體)
jlg.setFont(new Font("宋體",Font.PLAIN,12));
jlg.setBounds(190,210,49,19);
ct.add(jlg);
//jcl = new JButton("退出");
jcl.setForeground(Color.blue);//設置前景色(字體)
jcl.setFont(new Font("宋體",Font.PLAIN,12));
jcl.setBounds(270,210,49,19);
ct.add(jcl);

//創建BackImage對象
BackImage bi = new BackImage();
//確定背景圖片的位置
bi.setBounds(0, 0, 500, 300);
ct.add(bi);//把bi加入容器

setBounds(0,0, 350,250);
setUndecorated(true);//不使用上下修飾框
this.setSize(500,300);

//得到屏幕的寬度和高度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
setLocation(width/4,height/3);
setAlwaysOnTop(true);//設置面板總在最前面顯示
ct.addMouseMotionListener(this);
ct.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
//按下(mousePressed 不是點擊,而是鼠標被按下沒有擡起)
originX = e.getX();
originY = e.getY();
}
});
setVisible(true);
}

//內部類
class BackImage extends JPanel{
Image im;
//構造函數
public BackImage(){
try {
//添加圖片
im = ImageIO.read(new File("images//1.JPG"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//畫出圖片
public void paintComponent(Graphics g){
g.drawImage(im,0,0,500,300, this);
}
}

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
//拖動(mouseDragged 指的不是鼠標在窗口中移動,而是用鼠標拖動)
p1 = new Point();
p1 =this.getLocationOnScreen();
//e.getX()、e.getY()是現對於窗體的位置,
//e.getXOnScreen()、e.getYOnScreen()是相對屏幕的位置
//設置窗口的位置
//窗口當前的位置 + 鼠標當前在窗口的位置 - 鼠標按下的時候在窗口的位置
setLocation(p1.x+e.getX()-originX,p1.y+e.getY()-originY);
}


@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章