1

我对 Java 比较陌生,我正在尝试创建一个名为的类VisualObject,其实例可以在 s 之间拖放JPanel

这是 的getTransferData函数VisualObject,它实现Transferable

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

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

但是,在TransferHandlerfor my JPanels 我运行这条线

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(在其内容窗格中保存VisualObjects)和Main,它只创建两个ObjectFrames。

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();
    }

}
4

1 回答 1

1

关于SimplifiedVisualObject版本:getTransferData()方法 return this。由于您正在创建两个不同的实例,因此您可以有效地比较a.equals(b)它们通常总是错误的,除非equals()被覆盖。

关于您最初的问题:如果您调试代码,您会发现VisualObject. 每次调用时getTransferData()都会返回不同的实例,因此equals()比较返回 false。

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

也许当使用未知风格时,默认的拖放实现不知道如何处理它。结果,可传输对象在传输过程中被序列化和反序列化,导致总是创建不同的实例。

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

于 2013-07-14T21:56:51.270 回答