效果图
代码
/**
* 设置一个公共窗体面板
* 要求:图标,标题,最小化,最大化,还原,关闭,背景色,宽,高,初始定位,窗体上面可拖拽移动窗体位置,可拖拽边框放大缩小窗体大小
*/
public class publicFrame extends JFrame implements MouseListener/*ActionListener*/ {
private static final long serialVersionUID = 1L;
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取当前屏幕的工具包对象
private int frameState=0;
// 设置窗体位置
private int Location_x = (int) (toolkit.getScreenSize().getWidth() - 500) / 2;
private int Location_y = (int) (toolkit.getScreenSize().getHeight() - 500) / 2;
static Point origin = new Point();//变量,用于可拖动窗体 相当于下面的mouseAtX,mouseAtY
// int mouseAtX = 0;
// int mouseAtY = 0;
private JLabel jan, bi, da, title, theam, tip;//最小化,关闭,放大,标题,主题头像,提示
private JPanel head, body;
public publicFrame(String title_) {
//这个参数也是标题
this.setUndecorated(true);//去顶部 让窗体的标题栏不可见
this.setLayout(new BorderLayout());
head = new JPanel();
body = new JPanel();
head.setLayout(new headLayout());
head.setPreferredSize(new Dimension(this.getWidth(), 36));
//修改电脑任务栏中此窗口的图标
Image image = new ImageIcon("loginicon.png").getImage();
setIconImage(image);
theam = new JLabel(new ImageIcon("src/素材/头像白3.png"));
title = new JLabel(title_);
tip = new JLabel(new ImageIcon("src/素材/提醒.png"));
jan = new JLabel(new ImageIcon("src/素材/最小化.png"));
da = new JLabel(new ImageIcon("src/素材/放大.png"));
bi = new JLabel(new ImageIcon("src/素材/关闭.png"));
title.setForeground(Color.WHITE);
theam.addMouseListener(this);//这里的参数this就是MouseListener的实现方法
title.addMouseListener(this);
tip.addMouseListener(this);
jan.addMouseListener(this);
da.addMouseListener(this);
bi.addMouseListener(this);
head.addMouseListener(this);
this.addMouseListener(this);
/*
*鼠标监听器
*/
this.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) {
//监听鼠标移动
System