<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    常用鏈接

    統(tǒng)計

    最新評論

    在JAVA中使用拖拽功能(2轉(zhuǎn))

    在JAVA中使用拖拽功能
    sun在java2中引入了一些新的方法來幫助實現(xiàn)拖拽功能,這些新的類在java.awt.dnd包中
    實現(xiàn)一個D&D操作一般包括三個步驟:
     首先實現(xiàn)一個拖拽源,這個拖拽源和相應(yīng)的組件是關(guān)聯(lián)起來的
     第二步實現(xiàn)一個拖拽目標(biāo),這個目標(biāo)用來實現(xiàn)拖拽物的接收
     第三步實現(xiàn)一個數(shù)據(jù)傳輸對象,該對象封裝拖動的數(shù)據(jù)
      _____________________                                      _____________________
     |                     |                                     |                    |   
     | DragSource Component|                                     |DropTarget Component|
     |_____________________|                                     |____________________|
                       |                                              |
                       |____________Transferable Data_________________|
                       
     Transferable 接口實現(xiàn)出的對象能夠保證  DropTarget Component讀懂拖拽過來的對象中包含的信息
     如果是在同一個虛擬機中實現(xiàn)拖拽的話,DragSource Component會傳遞一個引用給DropTarget Component
     但是如果在不同的JVM中或者是在JVM和本地系統(tǒng)之間傳遞數(shù)據(jù)的話我們就必須實現(xiàn)一個Transferable對象來傳遞數(shù)據(jù)
     Transferable中封裝的內(nèi)容存放到DataFlavors,用戶可以通過訪問DataFlavors來獲取數(shù)據(jù)
     1。創(chuàng)建可拖拽對象
       一個對象那個如果想作為拖拽源的話,必須和五個對象建立練習(xí),這五個對象分別是:
        * java.awt.dnd.DragSource
        獲取DragSource的方法很簡單,直接調(diào)用DragSource.getDefaultDragSource();就可以得到DragSource對象
        * java.awt.dnd.DragGestureRecognizer
        DragGestureRecognizer類中實現(xiàn)了一些與平臺無關(guān)的方法,我們?nèi)绻朐谧约旱慕M件上實現(xiàn)拖拽的話只要調(diào)用createDefaultDragGestureRecognizer()方法就可以了
        該方法接收三個參數(shù),建立組件和拖拽動作之間的關(guān)系
        * java.awt.dnd.DragGestureListener
        當(dāng)建立了組件和拖拽動作之間的聯(lián)系后,如果用戶執(zhí)行了拖拽操作,組件將發(fā)送一個消息給DragGestureListener監(jiān)聽器
        DragGestureListener監(jiān)聽器接下來會發(fā)送一個startDrag()消息給拖拽源對象,告訴組件應(yīng)該執(zhí)行拖拽的初始化操作了
        拖拽源會產(chǎn)生一個DragSourceContext對象來監(jiān)聽動作的狀態(tài),這個監(jiān)聽過程是通過監(jiān)聽本地方法DragSourceContextPeer來實現(xiàn)的
        * java.awt.datatransfer.Transferable
       
        * java.awt.dnd.DragSourceListener
        DragSourceListener接口負(fù)責(zé)當(dāng)鼠標(biāo)拖拽對象經(jīng)過組件時的可視化處理, DragSourceListener接口的顯示結(jié)果只是暫時改變組件的外觀
        同時他提供一個feedback,當(dāng)用戶的拖拽操作完成之后會收到一個dragDropEnd的消息,我們可以在這個函數(shù)中執(zhí)行相應(yīng)的操作
        再來回顧一下拖拽源的建立過程
        首先、 DragGestureRecognizer 確認(rèn)一個拖拽操作,同時告知 DragGestureListener.

        其次、 Assuming the actions and/or flavors are OK, DragGestureListener asks DragSource to startDrag().

        第三、 DragSource creates a DragSourceContext and a DragSourceContextPeer. The DragSourceContext adds itself as a DragSourceListener to the DragSourceContextPeer.

        第四、 DragSourceContextPeer receives state notifications (component entered/exited/is over) from the native system and delegates them to the DragSourceContext.

        第五、 The DragSourceContext notifies the DragSourceListener, which provides drag over feedback (if the DropTargetListener accepts the action). Typical feedback includes asking the DragSourceContext to change the cursor.

        最后、 When the drop is complete, the DragSourceListener receives a dragDropEnd notification message

    2。創(chuàng)建droppable Component
       創(chuàng)建一個 droppable Component必須和下面兩個對象發(fā)生關(guān)聯(lián)  
        * java.awt.dnd.DropTarget
        DropTarget構(gòu)造函數(shù)使DropTarget 和 DropTargetListener objects發(fā)生關(guān)聯(lián)
        Droptarget對象提供 setComponent 和addDropTargetListener 兩個方法
       
        * java.awt.dnd.DropTargetListener
        The DropTargetListener needs an association with the Component so that the Component can notify the DropTargetListener to display "drag under" effects during the operation. This listener, which can be conveniently created as an inner class, transfers the data when the drop occurs. Warning: The Component itself shouldn't be the listener, since this implies its availability for use as some other Component's listener.
     




    下面的例子演示了一個從樹中拖拽一個節(jié)點到文本域中

    package appletandservlet;

    import java.awt.*;

    import javax.swing.*;
    import com.borland.jbcl.layout.XYLayout;
    import com.borland.jbcl.layout.*;
    import java.awt.dnd.*;
    import java.awt.datatransfer.*;
    import java.io.*;
    import javax.swing.tree.*;

    public class DragAndDrop extends JFrame {

        XYLayout xYLayout1 = new XYLayout();
        JScrollPane jScrollPane1 = new JScrollPane();
        JTextArea jTextArea1 = new JTextArea();

        public DragAndDrop() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            getContentPane().setLayout(xYLayout1);


            jScrollPane1.getViewport().setBackground(new Color(105, 38, 125));
            jTextArea1.setBackground(Color.orange);
            jTextArea1.setToolTipText("");
            JTree jtr = new JTree();
            jtr.setBackground(Color.BLUE);
            jScrollPane1.getViewport().add(jtr);
            this.getContentPane().add(jTextArea1,
                                      new XYConstraints(238, 42, 140, 248));
            this.getContentPane().add(jScrollPane1,
                                      new XYConstraints(16, 42, 217, 249));

            DragSource dragSource = DragSource.getDefaultDragSource(); //創(chuàng)建拖拽源
            dragSource.createDefaultDragGestureRecognizer(jtr,
                    DnDConstants.ACTION_COPY_OR_MOVE,
                    new DragAndDropDragGestureListener()); //建立拖拽源和事件的聯(lián)系
          DropTarget dropTarget = new DropTarget(jTextArea1,
                new DragAndDropDropTargetListener());

        }

        private void jbInit() throws Exception {

        }

        public static void main(String[] args) {

            DragAndDrop dad = new DragAndDrop();
            dad.setTitle("拖拽演示");
            dad.setSize(400, 300);
            dad.setVisible(true);

        }
    }


    class DragAndDropDragGestureListener implements DragGestureListener {
        public void dragGestureRecognized(DragGestureEvent dge) {
            //將數(shù)據(jù)存儲到Transferable中,然后通知組件開始調(diào)用startDrag()初始化
            JTree tree = (JTree) dge.getComponent();
          TreePath path = tree.getSelectionPath();
           if(path!=null){
               DefaultMutableTreeNode selection = (DefaultMutableTreeNode) path
                .getLastPathComponent();
               DragAndDropTransferable dragAndDropTransferable = new
                       DragAndDropTransferable(selection);
               dge.startDrag(DragSource.DefaultCopyDrop, dragAndDropTransferable, new DragAndDropDragSourceListener());
           }
        }


    }


    class DragAndDropTransferable implements Transferable {
        private DefaultMutableTreeNode treeNode;
        DragAndDropTransferable(DefaultMutableTreeNode treeNode) {
            this.treeNode = treeNode;
        }
        static DataFlavor flavors[] = {DataFlavor.stringFlavor};
        public DataFlavor[] getTransferDataFlavors() {
            return flavors;
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            if(treeNode.getChildCount()==0){
             return true;
            }
            return false;
        }

        public Object getTransferData(DataFlavor flavor) throws
                UnsupportedFlavorException, IOException {

          return treeNode;
       
        }


    }


     class DragAndDropDragSourceListener implements DragSourceListener {
      public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent) {
        if (dragSourceDropEvent.getDropSuccess()) {
            //拖拽動作結(jié)束的時候打印出移動節(jié)點的字符串
          int dropAction = dragSourceDropEvent.getDropAction();
          if (dropAction == DnDConstants.ACTION_MOVE) {
            System.out.println("MOVE: remove node");
          }
        }
      }

      public void dragEnter(DragSourceDragEvent dragSourceDragEvent) {
        DragSourceContext context = dragSourceDragEvent
            .getDragSourceContext();
        int dropAction = dragSourceDragEvent.getDropAction();
        if ((dropAction & DnDConstants.ACTION_COPY) != 0) {
          context.setCursor(DragSource.DefaultCopyDrop);
        } else if ((dropAction & DnDConstants.ACTION_MOVE) != 0) {
          context.setCursor(DragSource.DefaultMoveDrop);
        } else {
          context.setCursor(DragSource.DefaultCopyNoDrop);
        }
      }

      public void dragExit(DragSourceEvent dragSourceEvent) {
      }

      public void dragOver(DragSourceDragEvent dragSourceDragEvent) {
      }

      public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent) {
      }
    }
    class DragAndDropDropTargetListener implements DropTargetListener{
        public void dragEnter(DropTargetDragEvent dtde){
       
        }
        public void dragOver(DropTargetDragEvent dtde){
        }
        public void dropActionChanged(DropTargetDragEvent dtde){
        }
        public void dragExit(DropTargetEvent dte){
        }
        public void drop(DropTargetDropEvent dtde){
           Transferable tr=dtde.getTransferable();//使用該函數(shù)從Transferable對象中獲取有用的數(shù)據(jù)
           String s="";
            try {
                if(tr.isDataFlavorSupported(DataFlavor.stringFlavor)){
                    s = tr.getTransferData(DataFlavor.stringFlavor).toString();
                }
            } catch (IOException ex) {
            } catch (UnsupportedFlavorException ex) {
            }
            System.out.println(s);
            DropTarget c=(DropTarget)dtde.getSource();
            JTextArea d=(JTextArea)c.getComponent();
            if(s!=null&&s!=""){
                d.append(s + "\n");
            }
        }


    }

    posted on 2008-01-10 14:16 九寶 閱讀(2479) 評論(2)  編輯  收藏 所屬分類: Java

    評論

    # re: 在JAVA中使用拖拽功能(2轉(zhuǎn)) 2009-06-03 00:48 陳夏飛

    你好,請問如果想在JAVA中實現(xiàn)把文字拖入jpane該如何實現(xiàn),而且在這個Jpane里還有一個繪制有3d圖形的Canvas3d.
    我的郵箱:cxf.efrei@gamil.com
    這個問題困擾我很久,望高手能幫助我下,不勝感激。  回復(fù)  更多評論   

    # re: 在JAVA中使用拖拽功能(2轉(zhuǎn))[未登錄] 2010-10-15 10:45 jay

    2010最新日賺300元的項目|只需要每天掛機10小時就可以賺到300元|完全0投資,0風(fēng)險。詳情查看:http://www.31do.info/?203047.htm  回復(fù)  更多評論   

    主站蜘蛛池模板: 午夜免费国产体验区免费的| 亚洲一区中文字幕在线电影网| 精品国产日韩亚洲一区91| 久久久高清免费视频| 亚洲最大的视频网站| 免费看h片的网站| 亚洲成人福利网站| 美女视频黄a视频全免费| 亚洲卡一卡二卡乱码新区| 成熟女人牲交片免费观看视频| 色在线亚洲视频www| 毛片免费视频在线观看| 亚洲欧美国产欧美色欲| 亚洲第一视频在线观看免费| 中文字幕免费在线看电影大全 | 永久免费毛片手机版在线看| 亚洲欧美成人av在线观看| 免费一看一级毛片人| 国产亚洲精品免费视频播放| 亚洲一区二区三区电影| 成视频年人黄网站免费视频| 亚洲成av人无码亚洲成av人| 美腿丝袜亚洲综合| 在线人成精品免费视频| 亚洲乱理伦片在线观看中字| 亚洲成A∨人片天堂网无码| 中国国产高清免费av片| 亚洲国产成人手机在线电影bd| 性xxxx视频播放免费| 成人免费视频一区二区| 久久久无码精品亚洲日韩按摩| 性盈盈影院免费视频观看在线一区| 特级av毛片免费观看| 久久亚洲美女精品国产精品 | 亚洲综合综合在线| 免费无码看av的网站| 波多野结衣免费一区视频| 亚洲宅男精品一区在线观看| 亚洲毛片不卡av在线播放一区| 免费女人高潮流视频在线观看| 亚洲色无码专区一区|