我正在开发一个可以将一个JLabel的内容放到另一个JLabel中的应用程序。为此,我使用了NameSlot类:

public class NameSlot extends JLabel implements DropTargetListener{

NameSlot(String name){
    super(name);
    new DropTarget(this,this);

    this.setTransferHandler(new TransferHandler("foreground"));
    MouseListener listener = new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        JLabel comp = (JLabel) me.getSource();
        TransferHandler handler = comp.getTransferHandler();
        handler.exportAsDrag(comp, me, TransferHandler.COPY);
      }
    };
    this.addMouseListener(listener);
}



@Override
public void dragEnter(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void dragExit(DropTargetEvent dte) {
    // TODO Auto-generated method stub

}


@Override
public void dragOver(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void drop(DropTargetDropEvent dtde) {

//      DropTarget dt = (DropTarget) dtde.getSource();
//      NameSlot ns  = (NameSlot) dt.getComponent();

    try {
        JLabel l  =(JLabel) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
        System.out.println("drop detected from "+l.getText()+" to "+this.getText());
    } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


@Override
public void dropActionChanged(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}

我就是这样初始化的:
    this.setLayout(null);
    NameSlot ns = new NameSlot("test");
    ns.setLocation(20, 20);
    this.add(ns);

    NameSlot ns2 = new NameSlot("test2");
    ns2.setLocation(20, 20);
    this.add(ns2);

在尝试使用dtde.getSource()并由于coolcfans注释而将其报废后,我现在尝试使用DropTargetEvent的Transferable获取拖动源。我得到例外:
java.awt.datatransfer.UnsupportedFlavorException: Unicode String
    at javax.swing.TransferHandler$PropertyTransferable.getTransferData(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(Unknown Source)
    at sun.awt.datatransfer.TransferableProxy.getTransferData(Unknown Source)
    at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source)
    at an.judosoft.view.bracketSheets.factory.NameSlot.drop(NameSlot.java:71)

有人知道吗?

最佳答案

以下作品:

我将this.setTransferHandler(new TransferHandler("foreground"))更改为this.setTransferHandler(new TransferHandler("text"))
并为DataFlavor使用了new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String"),尽管它返回的是String而不是JLabel。如果需要JLabel,则需要更改TransferHandler。

import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

public class NameSlot extends JLabel implements DropTargetListener {
    NameSlot(final String name) {
        super(name);
        new DropTarget(this, this);
        this.setTransferHandler(new TransferHandler("text"));
        final MouseListener listener = new MouseAdapter() {
            @Override
            public void mousePressed(final MouseEvent me) {
                final JLabel comp = (JLabel) me.getSource();
                System.out.println(comp);

                final TransferHandler handler = comp.getTransferHandler();
                handler.exportAsDrag(comp, me, TransferHandler.COPY);
            }
        };
        this.addMouseListener(listener);
    }

    @Override
    public void dragEnter(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dragExit(final DropTargetEvent dte) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dragOver(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    @Override
    public void drop(final DropTargetDropEvent dtde) {

        // DropTarget dt = (DropTarget) dtde.getSource();
        // NameSlot ns = (NameSlot) dt.getComponent();

        try {
            final String s = (String) dtde.getTransferable().getTransferData(
                    new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String"));

            System.out.println("drop detected from " + s + " to " + this.getText());
        }
        catch (final UnsupportedFlavorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (final ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void dropActionChanged(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    public static void main(final String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        final NameSlot ns = new NameSlot("test");
        frame.add(ns);

        final NameSlot ns2 = new NameSlot("test2");
        frame.add(ns2);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}

09-05 04:25