在java中跨平台处理拖放和上下文菜单事件

时间:2021-07-21 12:21:00

I am building a small GUI application where I use drag and drop internally on custom components. Now I want to have context menus on the components which have drag and drop enabled.

我正在构建一个小的GUI应用程序,我在内部使用拖放自定义组件。现在我想在启用了拖放功能的组件上安装上下文菜单。

Now my question is, how do I properly distinguish between these two events. For context menus there is an API function, but for DnD I did not find one. I used mouse down to trigger DnD, but with that for example on Windows context menus stop working because they are triggered on mouse up.

现在我的问题是,我如何正确区分这两个事件。对于上下文菜单,有一个API函数,但对于DnD我找不到。我使用鼠标按下来触发DnD,但是例如在Windows上下文菜单上停止工作,因为它们是在鼠标向上触发的。

2 个解决方案

#1


For DnD use a combination of mouse down and mouse move.

对于DnD,请使用鼠标按下和鼠标移动的组合。

For context menu use the mouse clicked event (so the menu comes up when the mouse button is released not when it's pressed).

对于上下文菜单,使用鼠标单击事件(因此,当释放鼠标按钮时,菜单会出现,而不是在按下时)。

#2


The Swing tutorial has sections on "How to Use Menus" and "Drag and Drop".

Swing教程有关于“如何使用菜单”和“拖放”的部分。

The section on menus show how to display a popup by checking the "isPopupTrigger" of the MouseEvent. Although since JDK5 this process is easier as the setComponentPopupMenu()method has been added.

菜单部分显示如何通过检查MouseEvent的“isPopupTrigger”来显示弹出窗口。虽然从JDK5开始,这个过程比较容易,因为添加了setComponentPopupMenu()方法。

The secton on DnD shows how to use the built in DnD support.

DnD上的secton显示了如何使用内置的DnD支持。

#1


For DnD use a combination of mouse down and mouse move.

对于DnD,请使用鼠标按下和鼠标移动的组合。

For context menu use the mouse clicked event (so the menu comes up when the mouse button is released not when it's pressed).

对于上下文菜单,使用鼠标单击事件(因此,当释放鼠标按钮时,菜单会出现,而不是在按下时)。

#2


The Swing tutorial has sections on "How to Use Menus" and "Drag and Drop".

Swing教程有关于“如何使用菜单”和“拖放”的部分。

The section on menus show how to display a popup by checking the "isPopupTrigger" of the MouseEvent. Although since JDK5 this process is easier as the setComponentPopupMenu()method has been added.

菜单部分显示如何通过检查MouseEvent的“isPopupTrigger”来显示弹出窗口。虽然从JDK5开始,这个过程比较容易,因为添加了setComponentPopupMenu()方法。

The secton on DnD shows how to use the built in DnD support.

DnD上的secton显示了如何使用内置的DnD支持。