java drop和drag label以加入正确的图像

时间:2014-03-04 01:59:41

标签: java

这段代码只允许我第二次拒绝一个字符串,试图在textArea中放入一个字符串。

public GridLayoutTest() {


JFrame frame = new JFrame("GridLayout test");


 connection = getConnection();
try {  
    statement = (PreparedStatement) connection
     result = statement.executeQuery();
    while (result.next()) {
    byte[] image = null;
    image = result.getBytes("image");
                JPanel cellPanel = new JPanel(new BorderLayout());
                cellPanel.add(cellLabel, BorderLayout.NORTH);
                cellPanel.add(droplabel, BorderLayout.CENTER);

                gridPanel.add(cellPanel);

    }

   }
    catch (SQLException e) {
    e.printStackTrace();}

 }

1 个答案:

答案 0 :(得分:3)

所以,有两件事,首先......

public void DropTargetTextArea(String string1, String string2) {

不是构造函数,它是一种方法,请注意void。这意味着它永远不会被调用。这也是DropTargetTextArea textArea = new DropTargetTextArea();工作的原因,当你认为它不应该。

其次,你没有维护对传入(想要成为)构造函数的值的引用,所以你以后无法引用它们......

您可以尝试使用类似......

的内容
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);

}

然后使用像...这样的东西。

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

drop方法中。

在您的dragEnterdragOverdropActionChanged方法中,您有机会使用dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);dtde.rejectDrag();等内容来接受或拒绝拖动操作p>

已更新测试代码

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) {
                }

            }

        }
    }
}