java JFrame 分层布局与窗口拖拽事件

  • Post author:
  • Post category:java


分层布局主要实现要依靠Undecorated与setBackground,最后放置Object.getLayerPane().add(标签/画板,new Integer(Integer.MIN_VALUE));


下面是主要的实现代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//包
public class Tests{
    //Target Graphics Address Info
    public static Dimension GetScreenBorderInfo= Toolkit.getDefaultToolkit().getScreenSize();//获取信息默认包
    public static int GetScreenWidth=(int)GetScreenBorderInfo.getWidth();//宽
    public static int GetScreenHeight=(int)GetScreenBorderInfo.getHeight();//高
    //获取屏幕大小信息
    public static boolean MousePressedJudge=false;
    private static final java.awt.Point GetToWindowsPositionInfo=new java.awt.Point();
    //储存鼠标点击前的位置
    public static void main(String[] SimpleWindowsMoveExec){

        ImageIcon DefaultOfTestsObject=new ImageIcon("图像地址");
        JLabel LoadGraphicsOfJLabel=new JLabel();
        LoadGraphicsOfJLabel.setLayout(null);
        LoadGraphicsOfJLabel.setBounds(0,0,DefaultOfTestsObject.getIconWidth()
                ,DefaultOfTestsObject.getIconHeight());
        //设置边界
        JFrame GraphicsMainWindows=new JFrame();
        GraphicsMainWindows.setUndecorated(true);
        //无原始装扮
        GraphicsMainWindows.setBackground(new Color(0,0,0, 0));
        //背景透明
        GraphicsMainWindows.setLayout(null);
        //设置自定义布局
        GraphicsMainWindows.setBounds(GetScreenWidth/2-DefaultOfTestsObject.getIconWidth()/2,
                GetScreenHeight/2-DefaultOfTestsObject.getIconHeight()/2,DefaultOfTestsObject.getIconWidth()
                ,DefaultOfTestsObject.getIconHeight());
        ///使图像总是位于最中间
        LoadGraphicsOfJLabel.setIcon(DefaultOfTestsObject);
        GraphicsMainWindows.getLayeredPane().add(LoadGraphicsOfJLabel,new Integer(Integer.MIN_VALUE));
        //接收布局放置图像
        GraphicsMainWindows.setVisible(true);
        GraphicsMainWindows.addMouseListener(new MouseAdapter() {
            //监听点击
            public void mousePressed(MouseEvent MousePressedFrontOfPosition){
                GetToWindowsPositionInfo.x=MousePressedFrontOfPosition.getX();
                GetToWindowsPositionInfo.y=MousePressedFrontOfPosition.getY();
                MousePressedJudge=true;
                //获取点击前的鼠标信息
            }
            public void mouseReleased(MouseEvent MouseReleasedFrontOfPosition){
                MousePressedJudge=false;
                //如果不属于点击状态
            }
        });
        GraphicsMainWindows.addMouseMotionListener(new MouseAdapter(){
            //监听窗口拖拽
            public void mouseDragged(MouseEvent MouseMotionTimeOfPosition){
                java.awt.Point ObtainWindowsOfPosition=GraphicsMainWindows.getLocation();
                //获取的是窗口的位置
                if (MousePressedJudge==true){
                    //判定是拖拽
                    GraphicsMainWindows.setLocation(ObtainWindowsOfPosition.x+MouseMotionTimeOfPosition.getX()-GetToWindowsPositionInfo.x,
                            ObtainWindowsOfPosition.y+MouseMotionTimeOfPosition.getY()-GetToWindowsPositionInfo.y);
                    //算法:窗口位置+拖拽位置-点击前的位置,因为你点击的位置属于窗口位置所一要减
                }
            }
        });
    }

}

如果需要限制窗口拖拽则在条件中设置区间界限.

if (MousePressedJudge==true And(( MouseMotionTimeOfPosition.getx<=xxx,MouseMotionTimeOfPosition.X>=xxx)and( MouseMotionTimeOfPosition.gety<=xxx,MouseMotionTimeOfPosition.y>=xxx))

原理都差异细微,可以将窗口对象更改成组件对象,将窗口的setLocation更改为setBounds或setLocation都行.



版权声明:本文为m0_61267721原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。