java 拖拽_JAVA拖拽

sun在java2中引入了一些新的方法来帮助实现拖拽功能,这些新的类在java.awt.dnd包中

实现一个D&D操作一般包括三个步骤:

首先实现一个拖拽源,这个拖拽源和相应的组件是关联起来的

第二步实现一个拖拽目标,这个目标用来实现拖拽物的接收

第三步实现一个数据传输对象,该对象封装拖动的数据

_____________________                                      _____________________

|                     |                                     |                    |

| DragSource Component|                                     |DropTarget Component|

|_____________________|                                     |____________________|

|                                              |

|____________Transferable Data_________________|

Transferable 接口实现出的对象能够保证  DropTarget Component读懂拖拽过来的对象中包含的信息

如果是在同一个虚拟机中实现拖拽的话,DragSource Component会传递一个引用给DropTarget Component

但是如果在不同的JVM中或者是在JVM和本地系统之间传递数据的话我们就必须实现一个Transferable对象来传递数据

Transferable中封装的内容存放到DataFlavors,用户可以通过访问DataFlavors来获取数据

1。创建可拖拽对象

一个对象那个如果想作为拖拽源的话,必须和五个对象建立联系,这五个对象分别是:

* java.awt.dnd.DragSource

获取DragSource的方法很简单,直接调用DragSource.getDefaultDragSource();就可以得到DragSource对象

* java.awt.dnd.DragGestureRecognizer

DragGestureRecognizer类中实现了一些与平台无关的方法,我们如果想在自己的组件上实现拖拽的话只要调用createDefaultDragGestureRecognizer()方法就可以了

该方法接收三个参数,建立组件和拖拽动作之间的关系

* java.awt.dnd.DragGestureListener

当建立了组件和拖拽动作之间的联系后,如果用户执行了拖拽操作,组件将发送一个消息给DragGestureListener监听器

DragGestureListener监听器接下来会发送一个startDrag()消息给拖拽源对象,告诉组件应该执行拖拽的初始化操作了

拖拽源会产生一个DragSourceContext对象来监听动作的状态,这个监听过程是通过监听本地方法DragSourceContextPeer来实现的

* java.awt.datatransfer.Transferable

* java.awt.dnd.DragSourceListener

DragSourceListener接口负责当鼠标拖拽对象经过组件时的可视化处理, DragSourceListener接口的显示结果只是暂时改变组件的外观

同时他提供一个feedback,当用户的拖拽操作完成之后会收到一个dragDropEnd的消息,我们可以在这个函数中执行相应的操作

再来回顾一下拖拽源的建立过程

首先、 DragGestureRecognizer 确认一个拖拽操作,同时告知 DragGestureListener.

其次、 Assuming the actions and/or flavors are OK, DragGestureListener asks DragSource to startDrag().

第三、 DragSource creates a DragSourceContext and a DragSourceContextPeer. The DragSourceContext adds itself as a DragSourceListener to the DragSourceContextPeer.

第四、 DragSourceContextPeer receives state notifications (component entered/exited/is over) from the native system and delegates them to the DragSourceContext.

第五、 The DragSourceContext notifies the DragSourceListener, which provides drag over feedback (if the DropTargetListener accepts the action). Typical feedback includes asking the DragSourceContext to change the cursor.

最后、 When the drop is complete, the DragSourceListener receives a dragDropEnd notification message

2。创建droppable Component

创建一个 droppable Component必须和下面两个对象发生关联

* java.awt.dnd.DropTarget

DropTarget构造函数使DropTarget 和 DropTargetListener objects发生关联

Droptarget对象提供 setComponent 和addDropTargetListener 两个方法

* java.awt.dnd.DropTargetListener

The DropTargetListener needs an association with the Component so that the Component can notify the DropTargetListener to display "drag under" effects during the operation. This listener, which can be conveniently created as an inner class, transfers the data when the drop occurs. Warning: The Component itself shouldn't be the listener, since this implies its availability for use as some other Component's listener.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值