問(wèn)題描述
在 Java 中,當(dāng)被拖動(dòng)的項(xiàng)目是源代碼控制本身時(shí),執(zhí)行拖放的最佳方式是什么?我知道控件也不過(guò)是數(shù)據(jù),但區(qū)別確實(shí)會(huì)對(duì) UI 產(chǎn)生影響.
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.
我正在創(chuàng)建一個(gè)紙牌風(fēng)格的游戲,其中我有從 JLabel 派生的 Card 類的卡片對(duì)象.我想將該卡拖放到一個(gè)尚未命名的目標(biāo)控件上,從而將其拖到另一個(gè)位置.在拖動(dòng)過(guò)程中,我希望卡片在視覺(jué)上隨鼠標(biāo)移動(dòng),并且在放下時(shí)我希望它移動(dòng)到此目標(biāo)對(duì)象或返回到其先前的位置.
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.
我進(jìn)行了各種 D-n-D 測(cè)試,但沒(méi)有發(fā)現(xiàn)任何符合 Java 的 D-D 規(guī)則的東西.
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 拖動(dòng)卡片對(duì)象,我只能創(chuàng)建卡片的幻影圖像而不是實(shí)體圖像.此外,光標(biāo)發(fā)生了變化,我寧愿它沒(méi)有(我想我可以解決這個(gè)問(wèn)題),并且源代碼控制仍然可見(jiàn)(盡管在拖動(dòng)過(guò)程中應(yīng)該很容易使其透明)
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)
另一方面,我可以通過(guò)監(jiān)聽(tīng) MouseMotionListener.mouseDragged() 事件并手動(dòng)將卡片移動(dòng)到新位置來(lái)漂亮地拖動(dòng)卡片.這很好用,但它沒(méi)有遵循正確的 D-n-D,因?yàn)檫@不會(huì)通知其他控件的拖動(dòng).我想我可以創(chuàng)建自己的系統(tǒng)來(lái)通知其他控件,但這不會(huì)使用 Java 真正的 D-n-D.此外,如果我將真正的 Java d-n-d 內(nèi)容與這種在 mouseDragged 期間從字面上移動(dòng)卡片的方法混合在一起,那么我認(rèn)為真正的 D-n-D 內(nèi)容將永遠(yuǎn)不會(huì)起作用,因?yàn)閺募夹g(shù)上講,鼠標(biāo)永遠(yuǎn)不會(huì)直接位于任何其他控件上,而不是被拖動(dòng)的卡片.這個(gè)方向看起來(lái)就像一個(gè)粗略的黑客攻擊.
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.
我希望這是有道理的.我一直在跟蹤樣本時(shí)遇到問(wèn)題,因?yàn)樗鼈兛雌饋?lái)都非常不同,而我花費(fèi)大量時(shí)間研究的樣本似乎在 D-n-D 在 1.4 版中進(jìn)行大修之前幾年就過(guò)時(shí)了.
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.
推薦答案
在單個(gè)應(yīng)用程序周圍而不是在應(yīng)用程序之間拖動(dòng)組件的一種方法是使用 JLayeredPane.例如,請(qǐng)?jiān)诖颂幉榭次业拇a:在屏幕上拖動(dòng) 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!):
這篇關(guān)于Java:如何將控件而不是其數(shù)據(jù)拖放到新位置?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!