简体   繁体   English

如何在Java中跨面板拖动组件

[英]How to drag components across panels in java

Is it possible to drag a JLabel from one panel to another? 是否可以将JLabel从一个面板拖到另一个面板? I can only drag the label within one panel. 我只能在一个面板中拖动标签。 When the label moves out of the burden, it just disappear. 当标签移出负担时,它就消失了。 I don't know what to do. 我不知道该怎么办。

You can easily do this in several ways. 您可以通过几种方式轻松地做到这一点。 One way is to 一种方法是

  • bump the label to the glass pane on mousePressed(...) 将标签mousePressed(...)的玻璃窗格上
  • drag it in the glass pane on mouseDragged(...) , 将其拖动到mouseDragged(...)的玻璃窗格中
  • then drop it down to whatever container the mouse is currently over on mouseReleased(...) . 然后将其放到mouseReleased(...)上鼠标当前位于的任何容器上。

You will have to remove the label from its current container when you start dragging it. 开始拖动标签时,您必须从其当前容器中删除该标签。 Normally we add it to the glass pane to allow for the illusion of been dragged across multiple components. 正常情况下,我们将其添加到玻璃窗格中,以便将其拖入多个组件的错觉。

Take a look at My drag is better then yours for an example. 看看我的阻力比你的好。 I'd also recommend that you have a dig around his site, he does some nice work with drag and drop 我还建议您对他的网站有所了解,他通过拖放可以做得很好

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

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