Java can be a drag in the window inside the window assembly

    Daily use a lot of software, which some of the components (images, buttons, etc.) can drag the window inside. This is how to achieve it? Here I will share what Java can drag components presentations.

    We mainly use Java to the inside of the mouse events.

    First to clarify, Java, there is a coordinate system within our screens. Wherein the left screen of the vertices (0,0) , from left to right as the x axis positive direction , from top to bottom as the y-axis positive direction , for example, this screen shot:

    We used JFrame method setLocation method (set the window position), the coordinates is set relative to the screen in this coordinate system. Left vertex position of the window is the window position. 

Of course, listeners can get the mouse coordinates of the mouse within the screen.

    Then, the Java for a window in which there is a window internal coordinate system. Wherein the left window of the vertices (0,0) , from left to right as the x axis positive direction , from top to bottom as the y-axis positive direction , for example the window:

Understand coordinates in the window, we can set the position of the window assembly.

Components are provided with location setBounds methods, for example to set the position of a JLabel:

JLabel l=new JLabel("组件");
l.setBounds(10,10,120,169);
//setBounds用法:组件对象名.setBounds(x坐标,y坐标,组件宽,组件高); 坐标是指这个窗口的坐标系的坐标

 It refers to a position above the assembly position of the left vertex located assembly (coordinates).

Assembly and window coordinates as shown below: 

 

Mouse listener through the window, we can also get the coordinates of the mouse inside the window is. 

同样的,任何一个窗口组件也有它的内部坐标系,组件左顶点为(0,0)自左向右为x轴正方向自上而下为y轴正方向如图:

 这样给组件添加鼠标监听器,还可以获取鼠标在这个组件内位置。

上面就介绍了屏幕坐标系(整个电脑屏幕而言)窗口坐标系(整个窗口而言)窗口组件坐标系(只对这个组件而言)三种坐标系,大家千万不能混淆!

明白了这些坐标的含义,那么思路就很清晰了:我们只要通过监听器获取鼠标点击、拖动时在各个坐标系内的实时位置坐标,再通过计算,然后setBounds实时设置组件位置,不就可以了吗?

首先,我们先设置两个全局int值,代表鼠标在组件内按下时的位置:

static int mlx;    //代表鼠标在组件里面的x坐标(组件坐标系)
static int mly;    //代表鼠标在组件里面的y坐标(组件坐标系)

再建立一个全局JLabel对象:

static JLabel l=new JLabel(new ImageIcon("图片路径"));

JLabel对象既可以设置文字内容也可以设置图片内容,此处不过多赘述。

我们用鼠标拖一个东西,肯定是先点击再(按住鼠标不放)拖动,所以先获取点击时位置,再获取拖动时的实时位置。(常识2333)

那么,先给JLabel对象添加一个鼠标监听器,获取鼠标先点击这个组件时鼠标所在组件坐标系内的位置坐标:

l.addMouseListener(new MouseAdapter() {        //鼠标点击事件
    public void mousePressed(MouseEvent e) {
        mlx=e.getPoint().x;
        mly=e.getPoint().y;
    }
});

然后,再给JLabel对象添加一个鼠标动作监听器,获取鼠标在拖动这个组件时鼠标所在组件坐标系内和屏幕坐标系内的实时位置坐标,并获取窗口在屏幕内的坐标,加以计算实时设置组件在窗口中的位置:

l.addMouseMotionListener(new MouseMotionAdapter() {        //鼠标拖动事件
    public void mouseDragged(MouseEvent e) {
        l.setBounds(e.getXOnScreen()-jf.getX()-mlx,e.getYOnScreen()-jf.getY()-mly,120,169);
        }
});

上面,e.getXOnScreen()是获取鼠标在屏幕的x坐标,e.getYOnScreen()是获取鼠标在屏幕的y坐标(屏幕坐标系内)

jf是我的窗口对象(JFrame)的名称,jf.getX()是获取窗口在屏幕内x坐标,jf.getY()是获取窗口在屏幕内y坐标(屏幕坐标系内)

根据这三个坐标,即可得出拖动后组件应该在的位置。

设置了这么两个鼠标监听器,就成功实现了组件拖动了!

效果:

完整代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ActCoTest {
	static int mlx;
	static int mly;
	static JLabel l=new JLabel(new ImageIcon("res\\drgt.png"));
	public static void main(String[] args) {
		JFrame jf=new JFrame();
		jf.setUndecorated(true);		//窗口去边框
		jf.setSize(750,500);
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension sc=kit.getScreenSize();
		jf.setLocation(sc.width/2-200,sc.height/2-100);
		l.setBounds(30,65,120,169);		//先初始化JLabel位置
		l.addMouseListener(new MouseAdapter() {		//鼠标点击事件
			public void mousePressed(MouseEvent e) {
				mlx=e.getPoint().x;		//获取鼠标在组件内x坐标并赋值给mlx(组件坐标系)
				mly=e.getPoint().y;		//获取鼠标在组件内y坐标并赋值给mly(组件坐标系)
			}
		});
		l.addMouseMotionListener(new MouseMotionAdapter() {		//鼠标拖动事件
			public void mouseDragged(MouseEvent e) {
				l.setBounds(e.getXOnScreen()-jf.getX()-mlx,e.getYOnScreen()-jf.getY()-mly,120,169);
			}
		});
		JPanel jp=new JPanel();
		jp.add(l);
		jp.setLayout(null);
		jf.getContentPane().add(jp);
		jf.show();
	}
}

 

发布了25 篇原创文章 · 获赞 28 · 访问量 13万+

Guess you like

Origin blog.csdn.net/yanhanhui1/article/details/104101063