How to drag an image from one panel to other(如何将图像从一个面板拖到另一个面板)
问题描述
我创建了一个框架,然后在其中创建了面板.我已将图像放在一个面板中,现在我需要将该图像拖到另一个面板中.我怎么能那样做.请作为初学者一步一步解释.
I had created a frame and then panels in it. I had placed an image in one panel, now I need to drag that image to another panel. How could I do that. Please explain step by step as am a beginner.
推荐答案
使用纯java、DragGestureListener
、DropTargetAdapter
和TransferHandler
的快速示例一个面板上的图像到另一个面板的 DnD 支持:
Quick example using pure java, DragGestureListener
, DropTargetAdapter
and TransferHandler
for DnD support of images on one panel to another:
在单击和拖动任何形状之前:
Before any shape is clicked and dragged:
选择绿色形状并拖动到上面的空白面板后:
after green shape is selected and dragged onto empty panel above:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
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.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.io.IOException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.TransferHandler.TransferSupport;
import javax.swing.border.TitledBorder;
public class Test {
public static void main(String[] args) {
createAndShowJFrame();
}
public static void createAndShowJFrame() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = createJFrame();
frame.setVisible(true);
}
});
}
private static JFrame createJFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setTitle("Test");
JPanel panel = createEmptyJPanel();
new MyDropTargetListener(panel);//this must be done or we wont be able to drop any image onto the empty panel
frame.add(panel, BorderLayout.CENTER);
try {
frame.add(createJLabelPanel(), BorderLayout.SOUTH);
} catch (Exception ex) {
ex.printStackTrace();
}
frame.pack();
return frame;
}
private static JPanel createEmptyJPanel() {
final JPanel p = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
p.setBorder(new TitledBorder("Drag Image onto this panel"));
TransferHandler dnd = new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
if (!support.isDrop()) {
return false;
}
//only Strings
if (!support.isDataFlavorSupported(DataFlavor.imageFlavor)) {
return false;
}
return true;
}
@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable tansferable = support.getTransferable();
Icon ico;
try {
ico = (Icon) tansferable.getTransferData(DataFlavor.imageFlavor);
} catch (Exception e) {
e.printStackTrace();
return false;
}
p.add(new JLabel(ico));
return true;
}
};
p.setTransferHandler(dnd);
return p;
}
private static JPanel createJLabelPanel() throws Exception {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Drag Image from here to Panel above"));
JLabel label1 = new JLabel(new ImageIcon(new URL("http://i.stack.imgur.com/gJmeJ.png")));
JLabel label2 = new JLabel(new ImageIcon(new URL("http://i.stack.imgur.com/8BGfi.png")));
JLabel label3 = new JLabel(new ImageIcon(new URL("http://i.stack.imgur.com/1lgtq.png")));
MyDragGestureListener dlistener = new MyDragGestureListener();
DragSource ds1 = new DragSource();
ds1.createDefaultDragGestureRecognizer(label1, DnDConstants.ACTION_COPY, dlistener);
DragSource ds2 = new DragSource();
ds2.createDefaultDragGestureRecognizer(label2, DnDConstants.ACTION_COPY, dlistener);
DragSource ds3 = new DragSource();
ds3.createDefaultDragGestureRecognizer(label3, DnDConstants.ACTION_COPY, dlistener);
panel.add(label1);
panel.add(label2);
panel.add(label3);
return panel;
}
}
class MyDropTargetListener extends DropTargetAdapter {
private DropTarget dropTarget;
private JPanel p;
public MyDropTargetListener(JPanel panel) {
p = panel;
dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY, this, true, null);
}
@Override
public void drop(DropTargetDropEvent event) {
try {
DropTarget test = (DropTarget) event.getSource();
Component ca = (Component) test.getComponent();
Point dropPoint = ca.getMousePosition();
Transferable tr = event.getTransferable();
if (event.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Icon ico = (Icon) tr.getTransferData(DataFlavor.imageFlavor);
if (ico != null) {
p.add(new JLabel(ico));
p.revalidate();
p.repaint();
event.dropComplete(true);
}
} else {
event.rejectDrop();
}
} catch (Exception e) {
e.printStackTrace();
event.rejectDrop();
}
}
}
class MyDragGestureListener implements DragGestureListener {
@Override
public void dragGestureRecognized(DragGestureEvent event) {
JLabel label = (JLabel) event.getComponent();
final Icon ico = label.getIcon();
Transferable transferable = new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (!isDataFlavorSupported(flavor)) {
return false;
}
return true;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return ico;
}
};
event.startDrag(null, transferable);
}
}
这篇关于如何将图像从一个面板拖到另一个面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将图像从一个面板拖到另一个面板
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01