Java拖放图像并在拖动时显示图像缩略图 [英] Java Drag and Drop an Image and Show the image thumbnail while dragging

查看:353
本文介绍了Java拖放图像并在拖动时显示图像缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有图像节点的网格布局,我想在我的应用程序中添加dnd功能,例如当我将图像节点拖放到目标JPanel时,会显示原始大小,删除等操作。我已经实现了与转移手段的方式,一个与DropTargets,DragSource等。但是我有两个问题,我想和你讨论。



a)最好使用哪两种方式?我明白第二种方式给了我更多的功能,而不是TransferHandler,而且最简单的使用方式。但是我可以用这两种方式来完成我的工作,有两种方法更有效,或者说是我喜欢的代码。



b)拖动发生时我想在光标附近显示一个图像。第一个虽然我使用的是JFrame的glassPane,并创建一个JCompoment来绘制正确的图像并跟随光标。可以这样做吗?之后,我在文档中看到,DragSource的startDrag方法得到一个dragImage参数,是为了我想要的原因,我尝试传递一个图像,但我没有看到任何结果。



--------------更新---------------------



对于第二个问题(b),我尝试使用glasspane,但是我在java DND和我的监听器之间的互通中有问题,以便在glasspane中显示图像缩略图。每次只有其中一个工作正常。



!!!
更具体地说,当我已经调用startDrag()方法时,mouseDragged事件没有被触发



谢谢你的时间。

解决方案

解决



最后,我自己找到解决方案。 p>

如果有任何人有同样的问题,我说的一点是,在startDrag()方法触发所有的mouseDraged事件之后,拖到拖动事件()(从DragSourceMotionListener),我使错误也可以使用MouseMotionListener中的void mouseDragged(MouseEvent e)。


I have a grid layout with image nodes and I want to add in my application the dnd functionality, for example when I drag and drop an image node to an target JPanel make some actions for example show in original size, delete etc. I have achieve this with to ways one with transferhandler and one with the DropTargets, DragSource etc.But I have two questions and I want to discuss them with you.

a)Which of the two ways it's better to use? I understand that the second way give me more features than the TransferHandler and the first it's simplest to use. But I can do my work both of these ways, there is a more effective of two, or it's all about what I prefer to code.

b)While the dragging occurs I want to display an image near the cursor. The first though I make is to use the glassPane of the JFrame and create a JCompoment which paints proper image and follow the cursor.Can I make it with this way? After this I see in the documentation that the startDrag method of the DragSource get an dragImage argument, is this for the reason I want, I try to pass an Image but I not see any results.

--------------UPDATE---------------------

For the second question (b) I tried with the glasspane but I have problem in the interworking between java DND and my listeners for display the image thumb in the glasspane. each time only one of them work properly.

!!! More Specifically the mouseDragged event not fired when I have already call the startDrag() method

Thank you for your time.

解决方案

Solved

Finally, I find the solution myself.

If anyone has the same problem I say that the point is that after the startDrag() method fired all the mouseDraged events goes to dragMouseMoved()(from DragSourceMotionListener) and I make the error to use also the "void mouseDragged(MouseEvent e)" from the MouseMotionListener.

这篇关于Java拖放图像并在拖动时显示图像缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆