我基本上试图拖放包含Jlabel的JPanel:
以下是我实施的拖放代码示例:
public class TestDragAndDropPanel extends JPanel{
private static final long serialVersionUID = 1L;
Border borderContainer = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK);
private DataFlavor dataFlavor = new DataFlavor(TestDragAndDropPanel.class, "TestDragAndDropPanel");
public TestDragAndDropPanel(){
JPanel mainDragPanel = new JPanel();
mainDragPanel.setBorder(borderContainer);
JPanel mainDropPanel = new JPanel();
mainDropPanel.setBorder(borderContainer);
JPanel draggablePanel = new JPanel();
draggablePanel.setBorder(borderContainer);
JLabel draggableTitle = new JLabel("This is Draggable");
JLabel draggableTitle2 = new JLabel("Another Component");
draggablePanel.add(draggableTitle);
draggablePanel.add(draggableTitle2);
JPanel droppablePanel = new JPanel();
droppablePanel.setBorder(borderContainer);
JLabel droppableTitle = new JLabel("This is Droppable");
JLabel droppableTitle2 = new JLabel("Another Component 2");
droppablePanel.add(droppableTitle);
droppablePanel.add(droppableTitle2);
mainDragPanel.add(draggablePanel);
mainDropPanel.add(droppablePanel);
this.add(mainDragPanel);
this.add(mainDropPanel);
init(mainDropPanel, draggablePanel);
}
private void init(JPanel mainDropPanel, JPanel draggablePanel) {
DragSource ds = new DragSource();
ds.createDefaultDragGestureRecognizer(draggablePanel, DnDConstants.ACTION_MOVE, new DragGestureListImp());
new MyDropTargetListImp(mainDropPanel);
}
private class TransferablePanel implements Transferable {
private JPanel transferablePanel;
public TransferablePanel(JPanel transferablePanel) {
this.transferablePanel = transferablePanel;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { dataFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(dataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (flavor.equals(dataFlavor)){
return transferablePanel;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class DragGestureListImp implements DragGestureListener {
@Override
public void dragGestureRecognized(DragGestureEvent event) {
Cursor cursor = null;
JPanel panel = (JPanel) event.getComponent();
if (event.getDragAction() == DnDConstants.ACTION_MOVE) {
cursor = DragSource.DefaultMoveDrop;
}
event.startDrag(cursor, new TransferablePanel(panel));
}
}
private class MyDropTargetListImp extends DropTargetAdapter implements DropTargetListener {
private DropTarget dropTarget;
private JPanel panel;
public MyDropTargetListImp(JPanel panel) {
this.panel = panel;
dropTarget = new DropTarget(panel, DnDConstants.ACTION_MOVE, this,
true, null);
}
public void drop(DropTargetDropEvent event) {
try {
Transferable tr = event.getTransferable();
JPanel an = (JPanel) tr.getTransferData(dataFlavor);
if (event.isDataFlavorSupported(dataFlavor)) {
event.acceptDrop(DnDConstants.ACTION_MOVE);
this.panel.add(an);
event.dropComplete(true);
this.panel.validate();
this.panel.repaint();
return;
}
event.rejectDrop();
} catch (Exception e) {
e.printStackTrace();
event.rejectDrop();
}
}
}
public static void main(String[] args){
System.out.println("Application Running");
JPanel testDragAndDropPanel = new TestDragAndDropPanel();
JFrame mainframe = new JFrame();
mainframe.add(testDragAndDropPanel);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(300,200);
mainframe.setVisible(true);
}
}
基本上,似乎没有任何事情发生在下降方面,有人能告诉我为什么吗?
谢谢,
答案 0 :(得分:4)
你确定没有任何反应吗?如果在放下面板后最大化窗口,那么您将看到更改。问题是你没有重新验证下拉面板。
在drop
方法中,替换此部分
if (event.isDataFlavorSupported(dataFlavor)) {
event.acceptDrop(DnDConstants.ACTION_MOVE);
this.panel.add(an);
event.dropComplete(true);
this.panel.validate();
this.panel.repaint();
return;
}
用这个
if (event.isDataFlavorSupported(dataFlavor)) {
event.acceptDrop(DnDConstants.ACTION_MOVE);
panel.add(an);
panel.revalidate();
event.dropComplete(true);
return;
}