要把一个桌面上的文件拉进JAVA应用程序,显示文件路径,不容易啊~~
GOOGLE了不少,看了毫无头绪,还是看别人的实例比较好~
二个关键地方:
1.允许谁来接受DROP事件
自java.awt.Component以下都有setDropTarget(DropTarget)方法
实例DropTarget
DropTarget(Component c, DropTargetListener dtl)
这个C就是那个谁
DTL是由谁来处理DnD事件,可以自己写个类来实现此接口
2.处理DROP事件
实现DropTargetListener的类须实现以下5个方法:
dragEnter(DropTargetDragEvent dtde)
当DRAG进入C的Rectangle时的事件处理
dragExit(DropTargetEvent dte)
当DRAG退出C的Rectangle时的事件处理
dragOver(DropTargetDragEvent dtde)
Called when a drag operation is ongoing, while the mouse pointer is still over the operable part of the drop site for the DropTarget registered with this listener.
当DRAG不在C的Rectangle时事件处理
drop(DropTargetDropEvent dtde)
当DROP动作完成,就是松开鼠标的时候的事件处理.
我的显示文件路径什么的就写在这里.
dropActionChanged(DropTargetDragEvent dtde)
Called if the user has modified the current drop gestur.
老实说,我不知道这个方法有什么用.
请高手指点.
另外关于第一点,我找到好二个方法都有相同效果:
1.Component.setDropTarget(DropTarget)
2.直接NEW 一个DropTarget
打完收功~
本文介绍如何在Java应用程序中实现桌面文件的拖拽功能,并展示如何获取拖入文件的路径。主要涉及使用DropTarget组件及其实现监听器来处理拖放事件。
7948

被折叠的 条评论
为什么被折叠?



