java drop and drag label to join correct image

前端 未结 1 1525
天命终不由人
天命终不由人 2021-01-29 13:18

this code is only allowing me to reject a string the second time to try to drop in a textArea where there is all ready a string.

public GridLayoutTest() {


JFra         


        
1条回答
  •  情话喂你
    2021-01-29 13:28

    So, two things, first...

    public void DropTargetTextArea(String string1, String string2) {
    

    Isn't a constructor, it's a method, note the void. This means that it is never getting called. It's also the reason why DropTargetTextArea textArea = new DropTargetTextArea(); works, when you think it shouldn't.

    Second, you're not maintaining a reference to the values you pass in to the (want to be) constructor, so you have no means to references them later...

    You could try using something like...

    private String[] values;
    
    public DropTargetTextArea(String string1, String string2) {
    
        values = new String[]{string1, string2};
        DropTarget dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this, true);
    
    }
    

    And then use something like...

    if (values[0].equals(dragContents) || values[1].equals(dragContents)) {
    

    In the drop method.

    In your dragEnter, dragOver and dropActionChanged methods you have the oppurtunity to accept or reject the drag action using something like dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); or dtde.rejectDrag();

    Updated with test code

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.StringSelection;
    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.DragSourceDragEvent;
    import java.awt.dnd.DragSourceDropEvent;
    import java.awt.dnd.DragSourceEvent;
    import java.awt.dnd.DragSourceListener;
    import java.awt.dnd.DropTarget;
    import java.awt.dnd.DropTargetDragEvent;
    import java.awt.dnd.DropTargetDropEvent;
    import java.awt.dnd.DropTargetEvent;
    import java.awt.dnd.DropTargetListener;
    import java.io.IOException;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class DragAndDropExample {
    
        public static void main(String[] args) {
    
            ImageIcon ii1 = new ImageIcon("C:\\Users\\Desktop\\home.jpg");
            ImageIcon ii = new ImageIcon("C:\\Users\\Desktop\\images (2).jpg");
    // Create a frame
            JFrame frame = new JFrame("test");
            JLabel label = new JLabel(ii);
            JLabel label1 = new JLabel(ii1);
    
            JPanel panel = new JPanel(new GridLayout(2, 4, 10, 10));
    
            JLabel testLabel = new DraggableLabel("test");
            JLabel testingLabel = new DraggableLabel("testing");
            panel.add(testLabel);
    
            panel.add(testingLabel);
            panel.add(label);
            panel.add(label1);
    
            Component textArea = new DropTargetTextArea("test", "testing");
    
            frame.add(textArea, BorderLayout.CENTER);
            frame.add(panel, BorderLayout.SOUTH);
    
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    
        }
    
        public static class DraggableLabel extends JLabel implements DragGestureListener, DragSourceListener {
    
            DragSource dragSource1;
    
            public DraggableLabel(String text) {
    
                setText(text);
    
                dragSource1 = new DragSource();
    
                dragSource1.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
    
            }
    
            public void dragGestureRecognized(DragGestureEvent evt) {
    
                Transferable transferable = new StringSelection(getText());
    
                dragSource1.startDrag(evt, DragSource.DefaultCopyDrop, transferable, this);
            }
    
            public void dragEnter(DragSourceDragEvent evt) {
    
                System.out.println("Drag enter");
            }
    
            public void dragOver(DragSourceDragEvent evt) {
    
                System.out.println("Drag over");
            }
    
            public void dragExit(DragSourceEvent evt) {
    
                System.out.println("Drag exit");
            }
    
            public void dropActionChanged(DragSourceDragEvent evt) {
    
                System.out.println("Drag action changed");
            }
    
            public void dragDropEnd(DragSourceDropEvent evt) {
    
                System.out.println("Drag action End");
            }
    
        }
    
        public static class DropTargetTextArea extends JLabel implements DropTargetListener {
    
            private String[] values;
    
            public DropTargetTextArea(String string1, String string2) {
    
                values = new String[]{string1, string2};
    
                DropTarget dropTarget = new DropTarget(this, this);
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            public void dragEnter(DropTargetDragEvent evt) {
                if (!getText().isEmpty()) {
                    System.out.println("Reject drag enter");
                    evt.rejectDrag();
                } else {
                    evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                }
            }
    
            public void dragOver(DropTargetDragEvent evt) {
                if (!getText().isEmpty()) {
                    System.out.println("Reject drag over");
                    evt.rejectDrag();
                } else {
                    evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                }
            }
    
            public void dragExit(DropTargetEvent evt) {
    
                System.out.println("Drop exit");
            }
    
            public void dropActionChanged(DropTargetDragEvent evt) {
                if (!getText().isEmpty()) {
                    System.out.println("Reject dropActionChanged");
                    evt.rejectDrag();
                } else {
                    evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                }
            }
    
            public void drop(DropTargetDropEvent evt) {
    
                if (!getText().isEmpty()) {
    
                    evt.rejectDrop();
    
                } else {
    
                    try {
    
                        Transferable transferable = evt.getTransferable();
    
                        if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                            String dragContents = (String) transferable.getTransferData(DataFlavor.stringFlavor);
                            evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    
                            if (values[0].equals(dragContents) || (values[1]).equals(dragContents)) {
                                System.out.println("Accept Drop");
                                setText(getText() + " " + dragContents);
                                evt.getDropTargetContext().dropComplete(true);
                            } else {
                                System.out.println("Reject Drop");
                            }
                        }
    
                    } catch (IOException e) {
                        evt.rejectDrop();
                        evt.dropComplete(false);
                    } catch (UnsupportedFlavorException e) {
                    }
    
                }
    
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题