我是Java的新手,我正在尝试创建一个名为VisualObject的类,该类的实例可以在JPanel之间拖放。

这是实现getTransferDataVisualObjectTransferable函数:

    public Object getTransferData(DataFlavor d)
        throws UnsupportedFlavorException, IOException {

        if (d != visualObjectDataFlavor) {
            throw  new UnsupportedFlavorException(d);
        }
        return this;
   }


但是,在我的TransferHandlerJPanel中,我运行了这一行

System.out.println(t.getTransferData(VisualObject.visualObjectDataFlavor).equals(t.getTransferData(VisualObject.visualObjectDataFlavor)));


并且它总是返回false。我实际上如何才能将一个一致的指针返回到正在传输的对象?还是有更好的转移方式?

第二次编辑:

我的原始代码仍在下面,但是在经过简化的代码中仍然出现问题:

package trytwo;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class SimplifiedVisualObject implements Transferable {

    public static void main(String[] args) {
        SimplifiedVisualObject a, b;
        a = new SimplifiedVisualObject();
        b = new SimplifiedVisualObject();
        try {
            System.out.println(a.getTransferData(DataFlavor.imageFlavor).equals(b.getTransferData(DataFlavor.imageFlavor)));
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {
        return this;
    }

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

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return false;
    }

}


编辑:

下面是我编写的三个类:VisualObjectTransferable),ObjectFrame(在其内容窗格中保存VisualObject)和Main(仅创建两个ObjectFrame)。

VisualObject

package tryone;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.geom.Ellipse2D;
import java.io.IOException;
import javax.swing.JComponent;

public class VisualObject extends JComponent implements Transferable {

    private static final long serialVersionUID = -7105793092357377791L;
    public static DataFlavor visualObjectDataFlavor = new DataFlavor(VisualObject.class, "Visual Object");

    public VisualObject() {
        setOpaque(true);
        setLayout(null);
    }


    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.black);
        g2d.fill(new Ellipse2D.Double(0, 0,getWidth(),getHeight()));
    }

    @Override
    public Transferable getTransferData(DataFlavor d)
            throws UnsupportedFlavorException, IOException {

        if (d != visualObjectDataFlavor) {
            throw  new UnsupportedFlavorException(d);
        }
        return this;
    }


    @Override
    public DataFlavor[] getTransferDataFlavors() {
        DataFlavor d[] = {visualObjectDataFlavor};
        return d;
    }


    @Override
    public boolean isDataFlavorSupported(DataFlavor d) {
        if (d == visualObjectDataFlavor) {
            return false;
        }
        return false;
    }

    public VisualObject getThis() {
        return this;
    }
}


ObjectFrame

package tryone;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.TransferHandler;


public class ObjectFrame extends JFrame implements DragGestureListener {

    private static final long serialVersionUID = 4330669242071962048L;
    protected Cursor draggingCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    private JPanel panel;



    public ObjectFrame() {
        setVisible(true);
        setSize(300, 400);
        setLayout(new BorderLayout());
        setTitle("Object Tray");

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        panel = new JPanel();
        panel.setBackground(Color.white);
        panel.setLayout(null);
        add(panel, BorderLayout.CENTER);


        final DragSource ds = new DragSource();
        final DragGestureListener handle = this;

        panel.setTransferHandler(new ObjectTransferHandler());

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VisualObject v = null;


                v = new VisualObject();
                v.setSize(50, 50);
                v.setLocation(100, 100);
                ds.createDefaultDragGestureRecognizer(v,
                        DnDConstants.ACTION_COPY, handle);
                panel.add(v);

                panel.repaint();
            }
        });
    }

    @Override
    public void dragGestureRecognized(DragGestureEvent dge) {
        if (dge.getComponent() instanceof Transferable) {
            dge.startDrag(draggingCursor, (Transferable) dge.getComponent());
        }
    }

    private class ObjectTransferHandler extends TransferHandler {
        private static final long serialVersionUID = 5072686271469629699L;

        public ObjectTransferHandler() {
        }

        public boolean canImport(TransferSupport t) {
            if (t.isDataFlavorSupported(VisualObject.visualObjectDataFlavor))  {
                return true;
            }
            return false;
        }

        public boolean importData(TransferSupport ts) {
            Transferable t = ts.getTransferable();
            try {
                System.out.println(t.getTransferData(VisualObject.visualObjectDataFlavor) == (t.getTransferData(VisualObject.visualObjectDataFlavor)));

                return true;
            } catch (UnsupportedFlavorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
    }
}


Main

package tryone;

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        JFrame f1 = new ObjectFrame();
        JFrame f2 = new ObjectFrame();
    }

}

最佳答案

关于SimplifiedVisualObject版本:getTransferData()方法返回this。由于要创建两个不同的实例,因此您可以有效地比较a.equals(b),除非覆盖equals(),否则通常始终为false。

关于原始问题:如果调试代码,您会发现VisualObject也是这种情况。每次调用getTransferData()时,都会返回一个不同的实例,因此equals()比较将返回false。

如果您使用DataFlavor.imageFlavor,它将正常工作,尽管我无法确切解释原因。

也许在使用未知样式时,默认的拖放实现不知道如何处理它。结果,可转移对象在转移期间被序列化和反序列化,从而始终创建一个不同的实例。

编辑:实际上,答案是由Java tutorial that suggests给出的,当您知道仅在本地(在同一应用程序内)传输对象时,应创建新的DataFlavorvisualObjectDataFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=tryone.VisualObject");(将其作为静态字段,如在您的示例中,使用静态初始化程序块)。这将使系统知道您正在应用程序中传输本地对象,因此它将返回通过对象比较测试的相同实例。

09-27 10:42