简体   繁体   English

java用鼠标移动组件

[英]java move components with mouse

I tried to make any Component draggable by simply adding mouse listeners and using the setLocation function of java.awt.Component . 我试图通过简单地添加鼠标侦听器和使用java.awt.ComponentsetLocation函数来使任何Component可拖动。 I started with JButton to test if it were possible the way i thought. 我开始用JButton来测试它是否有可能像我想的那样。

Here is a code example for what I am trying to do: 这是我想要做的代码示例:

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

public class DragButton extends JButton{

private volatile int draggedAtX, draggedAtY;

public DragButton(String text){
    super(text);
    setDoubleBuffered(false);
    setMargin(new Insets(0, 0, 0, 0));
    setSize(25, 25);
    setPreferredSize(new Dimension(25, 25));

    addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            draggedAtX = e.getX() - getLocation().x;
            draggedAtY = e.getY() - getLocation().y;
        }
    });

    addMouseMotionListener(new MouseMotionAdapter(){
        public void mouseDragged(MouseEvent e){
            setLocation(e.getX() - draggedAtX, e.getY() - draggedAtY);
        }
    });
}

public static void main(String[] args){
    JFrame frame = new JFrame("DragButton");
    frame.setLayout(null);
    frame.getContentPane().add(new DragButton("1"));
    frame.getContentPane().add(new DragButton("2"));
    frame.getContentPane().add(new DragButton("3"));
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
}

Somehow this fails to work properly and I don't get why. 不知何故,这无法正常工作,我不明白为什么。 The actual distance dragged is half the distance of the mouse movement and it flickers around that distance while dragging as if two mouse positions are competing over the MouseMotionListener . 拖动的实际距离是鼠标移动距离的一半,并且在拖动时它会在该距离周围闪烁,就像两个鼠标位置在MouseMotionListener上竞争一样。

May anyone help a swing/awt noob? 愿任何人帮助挥杆/击球杆吗? =) Many thanks in advance. =)非常感谢提前。

Edit: 编辑:

Ok, so the problem was that I did not know that the event would refire at each mouse location with the position being relative (!) to the firing JComponent . 好的,所以问题是我不知道事件会在每个鼠标位置重新发送,其位置是相对 (!)到触发JComponent So this is the corrected and working code: 所以这是纠正和工作的代码:

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

public class DragButton extends JButton{

    private volatile int draggedAtX, draggedAtY;

    public DragButton(String text){
        super(text);
        setDoubleBuffered(false);
        setMargin(new Insets(0, 0, 0, 0));
        setSize(25, 25);
        setPreferredSize(new Dimension(25, 25));

        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                draggedAtX = e.getX();
                draggedAtY = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                setLocation(e.getX() - draggedAtX + getLocation().x,
                        e.getY() - draggedAtY + getLocation().y);
            }
        });
    }

    public static void main(String[] args){
        JFrame frame = new JFrame("DragButton");
        frame.setLayout(null);
        frame.getContentPane().add(new DragButton("1"));
        frame.getContentPane().add(new DragButton("2"));
        frame.getContentPane().add(new DragButton("3"));
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Thanks Adel for your efforts and mKorbel for the link. 感谢Adel的努力和mKorbel的链接。

You have to move with JComponent , I miss this definitions in voids mousePressed/mouseDragged ; 你必须使用JComponent移动,我在空洞中错过了这个定义mousePressed/mouseDragged ; in other hands, there nothing better around as @[camickr][1] excellent code for ComponentMover . 在另一方面,没有什么比@[camickr][1]ComponentMover优秀代码更好了。

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

public class movingButton extends JFrame{

    private JButton button ;

    public movingButton ()
    {
     super("Position helper");
       super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       super.setSize(500,520);
       super.setVisible(true);
       super.setLayout(null);
       button = new JButton ("drag me ");
       add(button);
       button.setBounds(100, 100, 150, 40);         
       button.addMouseMotionListener(new MouseAdapter(){

            public void mouseDragged(MouseEvent E)
            {
               int X=E.getX()+button.getX();
               int Y=E.getY()+button.getY;
               button.setBounds(X,Y,150,40);
            }
        });
    }

    public static void main (String x[])
    {           
        new movingButton();
    }
}

Why don't you use the java Transferable interface instead? 为什么不使用java Transferable接口呢?

Here's a tutorial on how to do it: http://www.javaworld.com/javaworld/jw-03-1999/jw-03-dragndrop.html 这是一个如何做的教程: http//www.javaworld.com/javaworld/jw-03-1999/jw-03-dragndrop.html

It is better if you would do 如果你愿意的话会更好

int X=E.getX() + button.getX();
int Y=E.getY() + button.getY();

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

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