简体   繁体   English

java jlabel单击/拖动

[英]java jlabel click / drag

The blue label is meant to move when you click and drag it. 单击并拖动时,蓝色标签即会移动。 This works but the x / y position then jumps in a funny way. 这行得通,但是x / y位置随后以一种有趣的方式跳跃。

Here's the code: 这是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class test extends JFrame implements MouseListener, MouseMotionListener {

private JPanel panel = new JPanel(null);    
private JLabel label1 = new JLabel();
private JLabel label2 = new JLabel();
private int mouseX = 200;
private int mouseY = 100;
private boolean drag = false;

public test() {
    this.add(panel);
    panel.setBackground(Color.WHITE);

    panel.add(label1);
    label1.setOpaque(true); 
    label1.setBackground(Color.BLUE);
    label1.setBounds(mouseX, mouseY, 100, 50);
    label1.addMouseMotionListener(this);
    label1.addMouseListener(this);

    panel.add(label2);
    label2.setOpaque(true); 
    label2.setBackground(Color.RED);
    label2.setBounds(mouseX + 200, mouseY, 100, 50);
    label2.addMouseMotionListener(this);
    label2.addMouseListener(this);
}

@Override
public void mousePressed(MouseEvent e) {
    if (e.getSource() == label1) {
        drag = true;
    } 
}

@Override
public void mouseReleased(MouseEvent e) {
    drag = false;
}

@Override
public void mouseDragged(MouseEvent e) {
    if (drag == true) {
        mouseX = e.getX();
        mouseY = e.getY();
        label1.setBounds(mouseX, mouseY, 100, 50);
    }
}

public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}

public static void main(String[] args) {
    test frame = new test();
    frame.setVisible(true);
    frame.setSize(600, 400);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Just put this on your MouseDragged method: 只需将其放在您的MouseDragged方法上:

public void mouseDragged(MouseEvent e) 
{
    if (drag == true) 
    {
        JComponent jc = (JComponent)e.getSource();
        jc.setLocation(jc.getX()+e.getX(), jc.getY()+e.getY());
    }
}

The coordinates returned by MouseEvent::getX() and MouseEvent::getY() represent the location of the event relative to the event's subject (ie relative to the label itself), which explains why your solution results in the label erratically jumping. MouseEvent :: getX()和MouseEvent :: getY()返回的坐标表示事件相对于事件主题(即,相对于标签本身)的位置,这解释了为什么您的解决方案导致标签不规则地跳跃。

By using MouseEvent::getComponent() to grab the label and then querying its position (possibly relative to the position when dragging commenced), you can devise a working solution. 通过使用MouseEvent :: getComponent()抓取标签,然后查询其位置(可能相对于拖动开始时的位置而言),可以设计一个可行的解决方案。

Your problem is your setting your bounds based on the mouse location in the MouseListener, but the MouseListener has its location relative to the JLabel itself, but the JLabel's location should be set relative to the panel. 您的问题是您根据MouseListener中的鼠标位置设置边界,但是MouseListener的位置相对于JLabel本身,但是JLabel的位置应相对于面板设置。 You'll need to do some simple vector addition to figure this out. 您需要执行一些简单的矢量加法运算来解决这个问题。

edit: oops, I didn't see that this was already answered, and they say the same thing... sorry. 编辑:哎呀,我没有看到这已经得到回答,他们说同样的话...对不起。

Maybe try adding something like that The red one will do it better 也许尝试添加类似的东西,红色的会做得更好

private int clicX = 0;
private int clicY = 0;

public void mousePressed(MouseEvent e) {
drag = true;
if (e.getSource() == label1) {
}
if (e.getSource() == label2) {
    clicX = e.getX();
    clicY = e.getY();
}
}

public void mouseDragged(MouseEvent e) {

        if (e.getSource() == label2) {

        JComponent jc = (JComponent)e.getSource();
        jc.setLocation(jc.getX()+e.getX()-clicX, jc.getY()+e.getY()-clicY);
        }

Create two global variables: 创建两个全局变量:

int x_pressed = 0;
int y_pressed = 0;

then create two events (mousePressed and mouseDragged over JLabel): 然后创建两个事件(在JLabel上使用mousePressed和mouseDragged):

lbl_banner.addMouseListener(new MouseAdapter()
{
    @Override
    public void mousePressed(MouseEvent e) {
        //catching the current values for x,y coordinates on screen
        x_pressed = e.getX();
        y_pressed = e.getY();
    }
});

lbl_banner.addMouseMotionListener(new MouseMotionAdapter(){
    @Override
    public void mouseDragged(MouseEvent e){
        //and when the Jlabel is dragged
        setLocation(e.getXOnScreen() - x_pressed, e.getYOnScreen() - y_pressed);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM