拖拽物品

传递参数物件ID值
修改部分代码:jmetest.awt.swingui.dnd.TestDnd
   protected void buildUI() {
            new JMEDragAndDrop(this.getDesktop());

            JInternalFrame frame = new JInternalFrame("dnd test", true, true);
            frame.setLayout(new GridLayout(4, 4));
            JInternalFrame frame1 = new JInternalFrame("dnd test1", true, true);
            frame1.setLayout(new GridLayout(4, 4));

            frame1.setIconifiable(true);
            Icon icon1 = getResizedIcon("kofei.png");
            frame.add(new DndIcon(this, icon1,201));
            Icon icon2 = getResizedIcon("Monkey.png");
            frame.add(new DndIcon(this, icon2,205));

            // few empty icons to let you play
            for (int i = 0; i < 13; i++) {
                frame.add(new DndIcon(this, null));
            }
            Icon icon3 = getResizedIcon("logo.jpg");
            frame.add(new DndIcon(this, icon3,209));


            DndIcon icons[]=new DndIcon[16];
            for (int i = 0; i < 16; i++) {
                icons[i]=new  DndIcon(this, null,i+1){

                    @Override
                    public void receive(int partId) {
                        System.out.println("received:"+partId);
                    }
                };
                frame1.add(icons[i]);
            }
            frame.setSize(64 * 4, 64 * 4);
            frame.setLocation(100, 100);
            frame.setVisible(true);

            frame1.setSize(64 * 4, 64 * 4);
            frame1.setLocation(320, 100);
            frame1.setVisible(true);

            this.getDesktop().getJDesktop().add(frame);
            this.getDesktop().getJDesktop().add(frame1);

            MouseInput.get().setCursorVisible(true);
        }

        private Icon getResizedIcon(String fileName) {
            ImageIcon icon = new ImageIcon(this.getClass().getResource("/jmetest/data/images/" + fileName));
            icon.setImage(icon.getImage().getScaledInstance(64, 64, 16));
            return icon;
        }
    }

    /**
     * DndIcon is the drag source and the drop target, so you can easily drag /
     * swap icons from different panels
     *
     * @author Nomis
     */
    public class DndIcon extends JLabel implements JMEDragGestureListener, JMEDragSourceListener, JMEDropTargetListener {

        private static final long serialVersionUID = 1L;
        private JMEDragAndDrop dndSupport;
        public int partID=0;
        public DndIcon(JMEDesktopState desktopSate, Icon icon) {
            this.setIcon(icon);
            this.dndSupport = desktopSate.getDesktop().getDragAndDropSupport();
            new JMEMouseDragGestureRecognizer(dndSupport, this, DnDConstants.ACTION_COPY_OR_MOVE, this);
            this.setBorder(BorderFactory.createLineBorder(Color.black));
        }

        public DndIcon(JMEDesktopState desktopSate, Icon icon,int partID) {
            this.setIcon(icon);
            this.dndSupport = desktopSate.getDesktop().getDragAndDropSupport();
            new JMEMouseDragGestureRecognizer(dndSupport, this, DnDConstants.ACTION_COPY_OR_MOVE, this);
            this.setBorder(BorderFactory.createLineBorder(Color.black));
            this.partID=partID;
        }

        /**
         *  接受一个物体
         */
        public void drop(JMEDropTargetEvent e) {
            TransferableImage t = (TransferableImage) e.getTransferable();
            Icon icon = null;
            try {
                icon = (Icon) t.getTransferData(null);
            } catch (UnsupportedFlavorException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println("------"+partID);
            DndIcon source = (DndIcon) e.getSource();
            receive(source.partID);
            if (icon != null) {
                // Set current icon to the source
                source.setIcon(this.getIcon());
                this.setIcon(icon);
            }

        }

        public void receive(int partId){

        }

        /**
         * 拉出的时候
         * @param dge
         */
        public void dragGestureRecognized(JMEDragGestureEvent dge) {

你可能感兴趣的:(拖拽)