問題描述
在 Java 中,當被拖動的項目是源代碼控制本身時,執行拖放的最佳方式是什么?我知道控件也不過是數據,但區別確實會對 UI 產生影響.
In Java, what is the best way to perform drag and drop when the item being dragged is the source control itself? I know a control is nothing but data too, but the difference does have UI impacts.
我正在創建一個紙牌風格的游戲,其中我有從 JLabel 派生的 Card 類的卡片對象.我想將該卡拖放到一個尚未命名的目標控件上,從而將其拖到另一個位置.在拖動過程中,我希望卡片在視覺上隨鼠標移動,并且在放下時我希望它移動到此目標對象或返回到其先前的位置.
I'm creating a solitaire-style game where I have card objects of class Card derived from JLabel. I want to drag that card to another location by dropping it onto a yet-to-be named Destination control. During the drag, I want the card to visually move with the mouse and when dropped I want it to either move to this destination object or return to its previous location.
我進行了各種 D-n-D 測試,但沒有發現任何符合 Java 的 D-D 規則的東西.
I've done various D-n-D tests and haven't found anything that works under the proper rules of Java's D-D.
例如,如果我使用真正的 D-n-D 拖動卡片對象,我只能創建卡片的幻影圖像而不是實體圖像.此外,光標發生了變化,我寧愿它沒有(我想我可以解決這個問題),并且源代碼控制仍然可見(盡管在拖動過程中應該很容易使其透明)
For example, if I drag the Card object using true D-n-D I can only create a ghosted image of the card and not a solid image. Also, the cursor changes and I'd rather it did not (I think I can fix that), and the source control remains visible (though it should be easy to make it transparent during the drag)
另一方面,我可以通過監聽 MouseMotionListener.mouseDragged() 事件并手動將卡片移動到新位置來漂亮地拖動卡片.這很好用,但它沒有遵循正確的 D-n-D,因為這不會通知其他控件的拖動.我想我可以創建自己的系統來通知其他控件,但這不會使用 Java 真正的 D-n-D.此外,如果我將真正的 Java d-n-d 內容與這種在 mouseDragged 期間從字面上移動卡片的方法混合在一起,那么我認為真正的 D-n-D 內容將永遠不會起作用,因為從技術上講,鼠標永遠不會直接位于任何其他控件上,而不是被拖動的卡片.這個方向看起來就像一個粗略的黑客攻擊.
On the other hand, I can drag the Card beautifully by listening for MouseMotionListener.mouseDragged() events and manually moving the Card to the new location. This works great, but it is not following proper D-n-D because this will not inform other controls of the drag. I figured I could either create my own system to notify the other controls, but that would not be using Java's real D-n-D. Also, if I mix the real Java d-n-d stuff with this method of literally moving the Card during mouseDragged then I assume the real D-n-D stuff will never work because the mouse will never technically be directly over any other control than the card being dragged. This direction just seems like a crude hack.
我希望這是有道理的.我一直在跟蹤樣本時遇到問題,因為它們看起來都非常不同,而我花費大量時間研究的樣本似乎在 D-n-D 在 1.4 版中進行大修之前幾年就過時了.
I hope this makes sense. I've been having problems following samples because they all seem very different, and one that I spent a great deal of time studying looks to be dated a couple years before D-n-D had its major overhaul in version 1.4.
推薦答案
在單個應用程序周圍而不是在應用程序之間拖動組件的一種方法是使用 JLayeredPane.例如,請在此處查看我的代碼:在屏幕上拖動 jlabel
One way to drag a component around a single application and not between applications is to use a JLayeredPane. For example please see my code here: dragging a jlabel around the screen
撲克牌示例可能如下所示(只要撲克牌圖像保持有效!):
An example with playing cards could look like this (as long as the playing card image remains valid!):
這篇關于Java:如何將控件而不是其數據拖放到新位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!