用于拖放的JLabel鼠标事件

时间:2011-03-15 08:30:10

标签: java swing

我希望通过覆盖JLabel上的鼠标事件来启用拖放功能,但是当我在mousePressed事件中定义拖放时,mouseReleased不会对该JLabel生效。我做错了吗?

            Thumbnails[I_Loop].setText("1");
            Thumbnails[I_Loop].setTransferHandler(new TransferHandler("text"));
            Thumbnails[I_Loop].addMouseListener( new MouseAdapter() {
                public void  mouseReleased(MouseEvent me) {
                       System.out.println("here mouse released");
                  }
                public void mousePressed(MouseEvent me) {
                    System.out.println("here mouse pressed");
                    JComponent comp = (JComponent) me.getSource();
                    TransferHandler handler = comp.getTransferHandler();
                    handler.exportAsDrag(comp, me, TransferHandler.COPY);
            });

*缩略图是JLabel的数组

运行程序时,拖放有效,但“此处鼠标已释放”语句无法打印。但是,当我从mousePressed()方法中删除负责DND的代码时,会打印“此处鼠标已释放”。

此代码有什么问题?

3 个答案:

答案 0 :(得分:12)

@Thomas是正确的,但有两个选择值得注意:

  • example显示了如何使用JLayeredPane拖动组件;这个variation扩展了这个概念;最近的example使用了类似的方法。

  • 以下代码显示了如何使用MouseMotionListener;这个更复杂的example使用相同的原则。

代码:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/5312702/230513 */
public class MouseDragTest extends JPanel {

    private static final String TITLE = "Drag me!";
    private static final int W = 640;
    private static final int H = 480;
    private Point textPt = new Point(W / 2, H / 2);
    private Point mousePt;

    public MouseDragTest() {
        this.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 32));
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                mousePt = e.getPoint();
                repaint();
            }
        });
        this.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                int dx = e.getX() - mousePt.x;
                int dy = e.getY() - mousePt.y;
                textPt.setLocation(textPt.x + dx, textPt.y + dy);
                mousePt = e.getPoint();
                repaint();
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(W, H);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int w2 = g.getFontMetrics().stringWidth(TITLE) / 2;
        g.drawString(TITLE, textPt.x - w2, textPt.y);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame(TITLE);
                f.add(new MouseDragTest());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:6)

好吧,如果我没记错的话,拖放机器会抓住所有鼠标事件并自行处理它们。因此,不再抛出正常的MouseEvents。您需要在JLabel DropTargetListener上注册DropTarget

答案 2 :(得分:0)

它必须是JLabel吗?我创建了一个带有字符串的类,可能会让你开始..

import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class mainProgram extends JPanel implements Runnable
{
    private static final long serialVersionUID = 1L;
    public static boolean MOUSE_DOWN = false;
    public static String str;

    public mainProgram() 
    {
        JFrame win = new JFrame("Window");

        win.add(this);

        win.setSize(700,500);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);
        str = "Drag me!";
        new Thread(this).start();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if(MOUSE_DOWN)
        {
            g.drawString(str, MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
        }
    }

    @Override
    public void run() 
    {
        Thread t = Thread.currentThread();

        this.addMouseListener(new MouseListener()
        {

            @Override
            public void mouseClicked(MouseEvent arg0) 
            {

            }

            @Override
            public void mouseEntered(MouseEvent arg0) 
            {

            }

            @Override
            public void mouseExited(MouseEvent arg0) 
            {

            }

            @Override
            public void mousePressed(MouseEvent arg0) 
            {
                MOUSE_DOWN = true;
            }

            @Override
            public void mouseReleased(MouseEvent arg0) 
            {
                MOUSE_DOWN = false;
            }

        });

        while(t==Thread.currentThread())
        {
            if(MOUSE_DOWN)
                repaint();

            try {Thread.sleep(10);}
            catch (InterruptedException e) {e.printStackTrace();}
        }   
    }
}