简体   繁体   English

如何使文件可拖离JPanel?

[英]How do I make a file draggable off of a JPanel?

I'm trying to make a program that allows users to drag files onto a window, it will then draw their icon/name where they dropped it. 我正在尝试制作一个程序,允许用户将文件拖到窗口上,然后将其放置在其图标/名称上。 This I have working. 这是我的工作。 My only issue is that I want the users to then be able to drag the File off of it (it will do something in the middle). 我唯一的问题是,我希望用户能够将文件拖离文件(它将在中间执行某些操作)。 So they can keep dragging a bunch of files on, then take them off (I also want it to look like it's supposed to, pic below). 因此,他们可以继续拖动一堆文件,然后将其取下(我也希望它看起来像应该的样子,如下图所示)。 I believe if you drag an item in a filechooser off of it, it will do what I want, I'm just not sure how they do it. 我相信,如果您将某个文件选择器中的某个项目从其上拖出,它将满足我的要求,我不确定他们是如何做到的。

Here's an example of me dragging a file onto the panel: 这是我将文件拖动到面板上的示例:
(source: gyazo.com ) (来源: gyazo.com

I want to be able to do the same thing, but with dragging it off. 我希望能够做同样的事情,但是要拖掉它。 Does anyone know how to go about doing this? 有人知道该怎么做吗? I'm using DragAndDrop listeners to detect when the users drag a file onto it, I'm thinking I should be able to sort've do the opposite, put my file onto draganddrop handler somehow. 我正在使用DragAndDrop侦听器来检测用户何时将文件拖到文件上,我想我应该能够进行相反的操作,以某种方式将文件放到拖放处理程序中。

Thanks :) 谢谢 :)

Figured it out myself a while ago, figured I'd post the answer here for anyone who wanted it; 我自己想了一下,想把答案贴在这里给任何想要的人。 had to make my own class which extended JLabel (I'm using a JLabel to display the icon), which implements Transferable, DragSourceListener, and DragGestureListener, and override the required methods of course. 必须创建自己的类来扩展JLabel(我正在使用JLabel来显示图标),该类实现Transferable,DragSourceListener和DragGestureListener,并且当然会覆盖所需的方法。 Took a while to work out all the kinks, but got it to work :) 花了一段时间解决所有问题,但让它起作用了:)

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

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