1

我基本上是在尝试拖放包含 Jlabel 的 JPanel:

这是我实现的拖放代码示例:

public class TestDragAndDropPanel extends JPanel{

private static final long serialVersionUID = 1L;
Border borderContainer = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK);
private DataFlavor dataFlavor = new DataFlavor(TestDragAndDropPanel.class, "TestDragAndDropPanel");


public TestDragAndDropPanel(){
            JPanel mainDragPanel = new JPanel();
    mainDragPanel.setBorder(borderContainer);

    JPanel mainDropPanel = new JPanel();
    mainDropPanel.setBorder(borderContainer);

    JPanel draggablePanel = new JPanel();
    draggablePanel.setBorder(borderContainer);
    JLabel draggableTitle = new JLabel("This is Draggable");
    JLabel draggableTitle2 = new JLabel("Another Component");
    draggablePanel.add(draggableTitle);
    draggablePanel.add(draggableTitle2);

    JPanel droppablePanel = new JPanel();
    droppablePanel.setBorder(borderContainer);
    JLabel droppableTitle = new JLabel("This is Droppable");
    JLabel droppableTitle2 = new JLabel("Another Component 2");
    droppablePanel.add(droppableTitle);
    droppablePanel.add(droppableTitle2);

    mainDragPanel.add(draggablePanel);
    mainDropPanel.add(droppablePanel);
    this.add(mainDragPanel);
    this.add(mainDropPanel);

    init(mainDropPanel, draggablePanel);
}

private void init(JPanel mainDropPanel, JPanel draggablePanel) {
    DragSource ds = new DragSource();
    ds.createDefaultDragGestureRecognizer(draggablePanel, DnDConstants.ACTION_MOVE, new DragGestureListImp());

    new MyDropTargetListImp(mainDropPanel);
}

private class TransferablePanel implements Transferable { 

    private JPanel transferablePanel;

    public TransferablePanel(JPanel transferablePanel) {
        this.transferablePanel = transferablePanel;
    } 

    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] { dataFlavor };
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(dataFlavor);
    }

    @Override
    public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {

        if (flavor.equals(dataFlavor)){
            return transferablePanel;
        } else {
            throw new UnsupportedFlavorException(flavor);
        }
    }
}

private class DragGestureListImp implements DragGestureListener {

    @Override
    public void dragGestureRecognized(DragGestureEvent event) {
        Cursor cursor = null;
        JPanel panel = (JPanel) event.getComponent();

        if (event.getDragAction() == DnDConstants.ACTION_MOVE) {
            cursor = DragSource.DefaultMoveDrop;
        }
        event.startDrag(cursor, new TransferablePanel(panel));
    }
}

private class MyDropTargetListImp extends DropTargetAdapter implements DropTargetListener {

    private DropTarget dropTarget;
    private JPanel panel;

    public MyDropTargetListImp(JPanel panel) {
        this.panel = panel;
        dropTarget = new DropTarget(panel, DnDConstants.ACTION_MOVE, this,
                true, null);
    }

    public void drop(DropTargetDropEvent event) {
        try {
            Transferable tr = event.getTransferable();
            JPanel an = (JPanel) tr.getTransferData(dataFlavor);

            if (event.isDataFlavorSupported(dataFlavor)) {
                event.acceptDrop(DnDConstants.ACTION_MOVE);
                this.panel.add(an);
                event.dropComplete(true);
                this.panel.validate();
                this.panel.repaint();
                return;
            }
            event.rejectDrop();
        } catch (Exception e) {
            e.printStackTrace();
            event.rejectDrop();
        }
    }
}

public static void main(String[] args){
    System.out.println("Application Running");
    JPanel testDragAndDropPanel =  new TestDragAndDropPanel();

    JFrame mainframe =  new JFrame();
    mainframe.add(testDragAndDropPanel);
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainframe.setSize(300,200);
    mainframe.setVisible(true);
}
}

基本上,下降方面似乎没有发生任何事情,谁能告诉我为什么?

谢谢,

4

1 回答 1

4

你确定什么都没发生吗?如果您在放下面板后最大化您的窗口,那么您会看到更改。问题是您没有重新验证下拉面板。

在你的drop方法中,替换这部分

if (event.isDataFlavorSupported(dataFlavor)) {
    event.acceptDrop(DnDConstants.ACTION_MOVE);
    this.panel.add(an);
    event.dropComplete(true);
    this.panel.validate();
    this.panel.repaint();
    return;
}

有了这个

if (event.isDataFlavorSupported(dataFlavor)) {
    event.acceptDrop(DnDConstants.ACTION_MOVE);
    panel.add(an);
    panel.revalidate();
    event.dropComplete(true);
    return;
}
于 2012-10-11T18:17:15.353 回答