package com.dqzt.views.mySwings;
import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.RoundRectangle2D;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyJFrame extends JFrame implements MouseMotionListener{
public static void main(String[] args) {
MyJFrame frame = new MyJFrame("",300,200);
frame.setVisible(true);
}
Point point = new Point(0, 0);
Container container = this.getContentPane();
public MyJFrame() {
}
public MyJFrame(String title,int x,int y){
JLabel titleJLabel = new JLabel(title);
int width = 15*title.length();
titleJLabel.setBounds(0, 0, width, 25);
this.setTitles(titleJLabel,x,y);
}
public MyJFrame(Icon icon,int x, int y){
JLabel titleJLabel = new JLabel(icon);
int width = icon.getIconWidth();
int heigth = icon.getIconHeight();
titleJLabel.setBounds(0, 0, width, heigth);
this.setTitles(titleJLabel,x,y);
}
public void setTitles(JLabel jLabel,int x, int y){
JLabel label = new JLabel();
label.add(jLabel);
container.add(label);
this.setUndecorated(true); //去边框。以实现后面的圆角等功能
this.setBounds(0, 0, x, y); //设置大小
this.setLocationRelativeTo(null); //居中显示
//设置圆角
com.sun.awt.AWTUtilities.setWindowShape(this,new RoundRectangle2D.Double(0,0,this.getWidth(),this.getHeight(),18,18));
this.addMouseMotionListener(this); //添加鼠标动作事件
}
public void mouseDragged(MouseEvent e) {// 鼠标拖动
Point newPoint = e.getPoint();// 获取新坐标
// 设置新位置
this.setLocation(this.getX() + (newPoint.x - point.x), this.getY()
+ (newPoint.y - point.y));
}
public void mouseMoved(MouseEvent e) {
point = e.getPoint();
}
}