好吧,所以我設置瞭如下。我有一個使用javax.swing的java 1.7項目。這是我在接下來的教程之外的第一個項目,它是那些熟悉桌面的人的主動跟蹤器。我有一個擴展JPanel的類(並且實現了Comparable <>),所以我可以動態地添加它併爲每個類添加故事不同的數據。爪哇,拖放改變面板的順序
面板被添加到佈局,其y位置取決於已經有多少面板。它們也被添加到ArrayList中按順序對它們進行排序。 該列表可以按類的其中一個屬性進行排序。
我現在遇到的問題是我想要手動拖放它們來更改佈局和ArrayList中的順序。還有一個附加說明,我不能刪除並重新添加一個,因爲我將失去班級中保存的值。
走向爲此我做了需要做下面的一個JButton:
- 只要按下鼠標按鈕,面板屬於需要用鼠標拖動。
- 當鼠標被釋放時,它需要檢查它的位置,如果有必要的話去那個位置。
我嘗試過的一些下降,拖累教程,但我似乎無法找到一個我可以調整到我想要的東西。
編輯1:這是我試過的。
MouseListener listener = new DragMouseAdapter();
dragButton.addMouseListener(listener);
dragButton.setTransferHandler(new TransferHandler("text"));
然後
class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
// TODO make it a swap location drag and drop
}
}
這是從一個教程,它成功地改變了文本和所有,但不是在所有我想要什麼,我不知道如何去改變它最多做我想做的事。
編輯2:This tutorial描述了我想要做的,除非它不起作用。它給出了以下錯誤:
Step 1 of 7: Mouse pressed. Going to export our RandomDragAndDropPanel so that it is draggable.
Step 2 of 7: Returning the acceptable TransferHandler action. Our RandomDragAndDropPanel accepts Copy only.
Step 3 of 7: Casting the RandomDragAndDropPanel as Transferable. The Transferable RandomDragAndDropPanel will be queried for acceptable DataFlavors as it enters drop targets, as well as eventually present the target with the Object it transfers.
Step 4 of 7: Querying for acceptable DataFlavors to determine what is available. Our example only supports our custom RandomDragAndDropPanel DataFlavor.
Problem lazy loading: RandomDragAndDropPanel
java.lang.ClassNotFoundException: RandomDragAndDropPanel
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.awt.datatransfer.DataFlavor.tryToLoadClass(Unknown Source)
at java.awt.datatransfer.DataFlavor.initialize(Unknown Source)
at java.awt.datatransfer.DataFlavor.<init>(Unknown Source)
at DnD.DragAndDropPanelsDemo.getDragAndDropPanelDataFlavor(DragAndDropPanelsDemo.java:197)
at DnD.RandomDragAndDropPanel.getTransferDataFlavors(DragAndDropPanelsDemo.java:392)
at sun.awt.datatransfer.DataTransferer.getFormatsForTransferable(Unknown Source)
at sun.awt.dnd.SunDragSourceContextPeer.startDrag(Unknown Source)
at java.awt.dnd.DragSource.startDrag(Unknown Source)
at java.awt.dnd.DragSource.startDrag(Unknown Source)
at java.awt.dnd.DragGestureEvent.startDrag(Unknown Source)
at javax.swing.TransferHandler$DragHandler.dragGestureRecognized(Unknown Source)
at java.awt.dnd.DragGestureRecognizer.fireDragGestureRecognized(Unknown Source)
at javax.swing.TransferHandler$SwingDragGestureRecognizer.gestured(Unknown Source)
at javax.swing.TransferHandler.exportAsDrag(Unknown Source)
at DnD.DraggableMouseListener.mousePressed(DragAndDropPanelsDemo.java:456)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
請,你能不能發佈您的嘗試,這樣我們可以幫助你找出什麼是錯的? – BackSlash 2014-12-02 08:54:15
我將編輯它,但問題大部分不是我的代碼不工作,但我不知道代碼應該是什麼。 – 2014-12-02 08:57:05