`
大器晚成
  • 浏览: 51413 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

拖拽物品

 
阅读更多
传递参数物件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) {
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics