我对 Java 比较陌生,我正在尝试创建一个名为的类VisualObject
,其实例可以在 s 之间拖放JPanel
。
这是 的getTransferData
函数VisualObject
,它实现Transferable
:
public Object getTransferData(DataFlavor d)
throws UnsupportedFlavorException, IOException {
if (d != visualObjectDataFlavor) {
throw new UnsupportedFlavorException(d);
}
return this;
}
但是,在TransferHandler
for my JPanel
s 我运行这条线
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;
}
}
编辑:
下面是我写的三个类:VisualObject
(Transferable
),ObjectFrame
(在其内容窗格中保存VisualObject
s)和Main
,它只创建两个ObjectFrame
s。
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();
}
}